Check if a file has been included before?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • D. Alvarado

    Check if a file has been included before?

    Hello,
    I want my scripts to be warning and error free. Is it possible to
    check is a file has already been included as part of a "require",
    "require_on ce" or "include" call in the script? I guess I could just
    include everything and turn warnings/errors off, but I'd prefer not to
    do that. Much thanks -
  • CountScubula

    #2
    Re: Check if a file has been included before?

    if you use requre or include _once() then they should never be included
    again.

    I do not know if there is a way to check, but years ago, in C i would write
    a constant in the include file and check if it was defined in the include if
    not, would include itself, then define.

    --
    Mike Bradley
    http://www.gzentools.com -- free online php tools
    "D. Alvarado" <laredotornado@ zipmail.com> wrote in message
    news:9fe1f2ad.0 403061949.1d74e bb6@posting.goo gle.com...[color=blue]
    > Hello,
    > I want my scripts to be warning and error free. Is it possible to
    > check is a file has already been included as part of a "require",
    > "require_on ce" or "include" call in the script? I guess I could just
    > include everything and turn warnings/errors off, but I'd prefer not to
    > do that. Much thanks -
    >[/color]


    Comment

    • Sims

      #3
      Re: Check if a file has been included before?

      [color=blue]
      > if you use requre or include _once() then they should never be included
      > again.
      >
      > I do not know if there is a way to check, but years ago, in C i would[/color]
      write[color=blue]
      > a constant in the include file and check if it was defined in the include[/color]
      if[color=blue]
      > not, would include itself, then define.
      >[/color]

      What about

      if( !defined('INCLU DE_THEFILE'))
      {
      include( 'somefile.php' );
      }else{
      echo 'Already included'
      }
      ....
      ....
      // in somefile.php
      defined('INCLUD E_THEFILE')

      Might not be the best way...i didn't know that including a file more than
      once could be a prob.

      Sims


      Comment

      • Mike Peters

        #4
        Re: Check if a file has been included before?

        On 2004-03-07, D. Alvarado wrote:[color=blue]
        > Hello,
        > I want my scripts to be warning and error free. Is it possible to
        > check is a file has already been included as part of a "require",
        > "require_on ce" or "include" call in the script? I guess I could just
        > include everything and turn warnings/errors off, but I'd prefer not to
        > do that. Much thanks -[/color]
        How about:
        get_included_fi les () or get_required_fi les ()

        They both return an array containing all included and required files.
        They both do the same thing by the way (one is an alias of the other).
        See:
        Returns an array with the names of included or required files


        --
        Mike Peters
        mike [-AT-] ice2o [-DOT-] com
        I am a DevOps and Cloud architecture consultant based in Northumberland in the UK. I provide consultancy on DevOps and private and public cloud solutions. I design and implement continous integration and continuous delivery solutions using Open Source, bespoke and commercial off the shelf software. I can also provide training and tuition for you or your team in DevOps best practices, tooling and solutions, OpenSource software, automation and cloud architecture solutions.

        Comment

        • CountScubula

          #5
          Re: Check if a file has been included before?

          "Sims" <siminfrance@ho tmail.com> wrote in message
          news:c2f05b$1rr 9hh$1@ID-162430.news.uni-berlin.de...[color=blue]
          >[color=green]
          > > if you use requre or include _once() then they should never be included
          > > again.
          > >
          > > I do not know if there is a way to check, but years ago, in C i would[/color]
          > write[color=green]
          > > a constant in the include file and check if it was defined in the[/color][/color]
          include[color=blue]
          > if[color=green]
          > > not, would include itself, then define.
          > >[/color]
          >
          > What about
          >
          > if( !defined('INCLU DE_THEFILE'))
          > {
          > include( 'somefile.php' );
          > }else{
          > echo 'Already included'
          > }
          > ...
          > ...
          > // in somefile.php
          > defined('INCLUD E_THEFILE')
          >
          > Might not be the best way...i didn't know that including a file more than
          > once could be a prob.
          >
          > Sims[/color]


          Yea thats it, then I started thinking, they should just use require/include
          ...._once(); then it hit me, instead of going through all the code, and
          writing snippets to see if it was already include, just change all includes
          to .._once().

          OK, as I write this, I just got an idea, perhaps a small file that shows
          something cool is included at several places, and they dont want to over do
          it, so they need to check if it is included.


          --
          Mike Bradley
          http://www.gzentools.com -- free online php tools


          Comment

          • Tim Van Wassenhove

            #6
            Re: Check if a file has been included before?

            On 2004-03-07, CountScubula <me@scantek.hot mail.com> wrote:[color=blue]
            > if you use requre or include _once() then they should never be included
            > again.
            >
            > I do not know if there is a way to check, but years ago, in C i would write
            > a constant in the include file and check if it was defined in the include if
            > not, would include itself, then define.[/color]

            I presume it was like

            #ifndef FOO_H
            #define FOO_H
            ....
            #endif

            --

            Comment

            • Stephen Gordon

              #7
              Re: Check if a file has been included before?


              "Mike Peters" <o0__mike__0oRE MOVE@THIShotmai l.com> wrote in message
              news:b9918d42ea e3f51b874c8a3f3 a42b05c@news.te ranews.com...[color=blue]
              > On 2004-03-07, D. Alvarado wrote:[color=green]
              > > Hello,
              > > I want my scripts to be warning and error free. Is it possible to
              > > check is a file has already been included as part of a "require",
              > > "require_on ce" or "include" call in the script? I guess I could just
              > > include everything and turn warnings/errors off, but I'd prefer not to
              > > do that. Much thanks -[/color]
              > How about:
              > get_included_fi les () or get_required_fi les ()
              >
              > They both return an array containing all included and required files.
              > They both do the same thing by the way (one is an alias of the other).
              > See:
              > http://www.php.net/get_included_files
              >
              > --
              > Mike Peters
              > mike [-AT-] ice2o [-DOT-] com
              > http://www.ice2o.com[/color]

              Why can't you just use include_once(.. .) and require_once(.. .) to include
              all files if you know some will be included multiple times?

              -Steve


              Comment

              Working...