require_once() and Subdirectories

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David T. Ashley

    require_once() and Subdirectories

    Hi,

    Does require_once() treat "file.inc" and "subdirecto ry/file.inc" as the same
    things or different?

    The reason for my question is that I'd like to organize my PHP library into
    subdirectories, and if I accidentally have a naming collision, I'm curious
    how require_once() will behave.

    Thanks.
    --
    David T. Ashley (dta@e3ft.com)
    http://www.e3ft.com (Consulting Home Page)
    http://www.dtashley.com (Personal Home Page)
    http://gpl.e3ft.com (GPL Publications and Projects)


  • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

    #2
    Re: require_once() and Subdirectories

    David T. Ashley wrote:
    Does require_once() treat "file.inc" and "subdirecto ry/file.inc" as the
    same things or different?
    Different, of course.
    The reason for my question is that I'd like to organize my PHP library
    into subdirectories, and if I accidentally have a naming collision, I'm
    curious how require_once() will behave.
    As usual: It will look for the specified filename in the current working
    directory, then in the directories specified in the "Include dirs"
    configuration directive.

    Please RTFM for specifics.

    --
    ----------------------------------
    Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

    Un ordenador no es un televisor ni un microondas, es una herramienta
    compleja.

    Comment

    • Rik

      #3
      Re: require_once() and Subdirectories

      On Thu, 07 Jun 2007 16:56:34 +0200, David T. Ashley <dta@e3ft.comwr ote:
      Hi,
      >
      Does require_once() treat "file.inc" and "subdirecto ry/file.inc" as the
      same
      things or different?
      Different, obviously, it searches another directory for the file instead
      of the current working directory.
      The reason for my question is that I'd like to organize my PHP library
      into
      subdirectories, and if I accidentally have a naming collision, I'm
      curious
      how require_once() will behave.
      It recognizes the exact file you use (how else could PHP actually open the
      included/required file?). So no matter how much files are named
      'class.php' or 'index.php', each will be recognised as different in their
      respective directory.

      So to answer the real question: you can divide your library into different
      directories all you like, files in different directories with the same
      name do not bother the include(_once() )/require(_once() ) functions.

      The only caveat is the if you're used to working with (and are depending
      on) a standard include_dir, you might get some unexpected results
      --
      Rik Wasmus

      Comment

      • phpCodeHead

        #4
        Re: require_once() and Subdirectories

        On Jun 7, 9:56 am, "David T. Ashley" <d...@e3ft.comw rote:
        Hi,
        >
        Does require_once() treat "file.inc" and "subdirecto ry/file.inc" as the same
        things or different?
        >
        The reason for my question is that I'd like to organize my PHP library into
        subdirectories, and if I accidentally have a naming collision, I'm curious
        how require_once() will behave.
        >
        Thanks.
        --
        David T. Ashley (d...@e3ft.com)http://www.e3ft.com (Consulting Home Page)http://www.dtashley.com (Personal Home Page)http://gpl.e3ft.com (GPL Publications and Projects)
        Taken directly from the manual.



        Please note that include(), include_once(), require(), and
        require_once() all function the same so far as in reference to your
        question.

        " Files for including are first looked in include_path relative to the
        current working directory and then in the directory of the current
        script. E.g. if your include_path is libraries, current working
        directory is /www/, you included include/a.php and there is include
        "b.php" in that file, b.php is first looked in /www/libraries/ and
        then in /www/include/. If filename begins with ./ or ../, it is looked
        only in include_path relative to the current working directory.

        When a file is included, the code it contains inherits the variable
        scope of the line on which the include occurs. Any variables available
        at that line in the calling file will be available within the called
        file, from that point forward. However, all functions and classes
        defined in the included file have the global scope. "

        HTH,

        Gene Kelley
        LAMP Software Developer
        BizFlowDesigns. com

        Comment

        • David T. Ashley

          #5
          Re: require_once() and Subdirectories

          "Iván Sánchez Ortega" <ivansanchez-alg@rroba-escomposlinux.-.punto.-.org>
          wrote in message news:f4979g$8j9 $1@hercules.coh p1...
          David T. Ashley wrote:
          >
          >Does require_once() treat "file.inc" and "subdirecto ry/file.inc" as the
          >same things or different?
          >
          Different, of course.
          >
          >The reason for my question is that I'd like to organize my PHP library
          >into subdirectories, and if I accidentally have a naming collision, I'm
          >curious how require_once() will behave.
          >
          As usual: It will look for the specified filename in the current working
          directory, then in the directories specified in the "Include dirs"
          configuration directive.
          >
          Please RTFM for specifics.
          Thanks for your reply.

          My question was subtly different.

          I have no doubt that require_once() will open and include the correct file.

          I was concerned about it possibly believing that "/dir1/file.inc" had been
          included when it was "/dir2/file.inc". In other words, I was concerned
          about the "once" part of it and how it remembers what has already been
          included.

          My question was about the "once" part of it.

          Thanks, Dave.


          Comment

          • David T. Ashley

            #6
            Re: require_once() and Subdirectories

            "phpCodeHea d" <phpcodehead@gm ail.comwrote in message
            news:1181229551 .203483.302990@ p77g2000hsh.goo glegroups.com.. .
            On Jun 7, 9:56 am, "David T. Ashley" <d...@e3ft.comw rote:
            >Hi,
            >>
            >Does require_once() treat "file.inc" and "subdirecto ry/file.inc" as the
            >same
            >things or different?
            >>
            >The reason for my question is that I'd like to organize my PHP library
            >into
            >subdirectories , and if I accidentally have a naming collision, I'm
            >curious
            >how require_once() will behave.
            >
            Taken directly from the manual.
            >

            >
            Please note that include(), include_once(), require(), and
            require_once() all function the same so far as in reference to your
            question.
            >
            " Files for including are first looked in include_path relative to the
            current working directory and then in the directory of the current
            script. E.g. if your include_path is libraries, current working
            directory is /www/, you included include/a.php and there is include
            "b.php" in that file, b.php is first looked in /www/libraries/ and
            then in /www/include/. If filename begins with ./ or ../, it is looked
            only in include_path relative to the current working directory.
            >
            When a file is included, the code it contains inherits the variable
            scope of the line on which the include occurs. Any variables available
            at that line in the calling file will be available within the called
            file, from that point forward. However, all functions and classes
            defined in the included file have the global scope. "
            Thanks for your reply.

            My question was subtly different.

            I have no doubt that require_once() will open and include the correct file.

            I was concerned about it possibly believing that "/dir1/file.inc" had been
            included when it was "/dir2/file.inc". In other words, I was concerned
            about the "once" part of it and how it remembers what has already been
            included.

            My question was about the "once" part of it and how that might interact with
            paths.

            Thanks, Dave.


            Comment

            • David T. Ashley

              #7
              Re: require_once() and Subdirectories

              "Rik" <luiheidsgoeroe @hotmail.comwro te in message
              news:op.ttj4mwa vqnv3q9@metalli um...
              On Thu, 07 Jun 2007 16:56:34 +0200, David T. Ashley <dta@e3ft.comwr ote:
              >
              >Hi,
              >>
              >Does require_once() treat "file.inc" and "subdirecto ry/file.inc" as the
              >same
              >things or different?
              >
              Different, obviously, it searches another directory for the file instead
              of the current working directory.
              >
              >The reason for my question is that I'd like to organize my PHP library
              >into
              >subdirectories , and if I accidentally have a naming collision, I'm
              >curious
              >how require_once() will behave.
              >
              It recognizes the exact file you use (how else could PHP actually open the
              included/required file?). So no matter how much files are named
              'class.php' or 'index.php', each will be recognised as different in their
              respective directory.
              >
              So to answer the real question: you can divide your library into different
              directories all you like, files in different directories with the same
              name do not bother the include(_once() )/require(_once() ) functions.
              >
              The only caveat is the if you're used to working with (and are depending
              on) a standard include_dir, you might get some unexpected results
              Thanks for your reply.

              My question was subtly different.

              I have no doubt that require_once() will open and include the correct file.

              I was concerned about it possibly believing that "/dir1/file.inc" had been
              included when it was "/dir2/file.inc". In other words, I was concerned
              about the "once" part of it and how it remembers what has already been
              included.

              My question was about the "once" part of it.

              Thanks, Dave.


              Comment

              • Rik

                #8
                Re: require_once() and Subdirectories

                On Thu, 07 Jun 2007 17:45:49 +0200, David T. Ashley <dta@e3ft.comwr ote:
                >>Does require_once() treat "file.inc" and "subdirecto ry/file.inc" as the
                >>same
                >>things or different?
                >>
                >Different, obviously, it searches another directory for the file instead
                >of the current working directory.
                >>
                >>The reason for my question is that I'd like to organize my PHP library
                >>into
                >>subdirectorie s, and if I accidentally have a naming collision, I'm
                >>curious
                >>how require_once() will behave.
                >>
                >It recognizes the exact file you use (how else could PHP actually open
                >the
                >included/required file?). So no matter how much files are named
                >'class.php' or 'index.php', each will be recognised as different in
                >their
                >respective directory.
                >>
                >So to answer the real question: you can divide your library into
                >different
                >directories all you like, files in different directories with the same
                >name do not bother the include(_once() )/require(_once() ) functions.
                >>
                >The only caveat is the if you're used to working with (and are depending
                >on) a standard include_dir, you might get some unexpected results
                >
                Thanks for your reply.
                >
                My question was subtly different.
                No, it wasn't.
                I have no doubt that require_once() will open and include the correct
                file.
                >
                I was concerned about it possibly believing that "/dir1/file.inc" had
                been
                included when it was "/dir2/file.inc". In other words, I was concerned
                about the "once" part of it and how it remembers what has already been
                included.
                >
                My question was about the "once" part of it.
                Which was answered by "It recognizes the exact file you use (...) So no
                matter how much files are named 'class.php' or 'index.php', each will be
                recognised as different in their respective directory.".

                You tell me how that could be any clearer?

                If you use get_included_fi les(), you'd see PHP stores the full path of all
                files, not just the filename.
                --
                Rik Wasmus

                Comment

                • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

                  #9
                  Re: require_once() and Subdirectories

                  David T. Ashley wrote:
                  Thanks for your reply.
                  Tip: you *don't* have to post the same message THREE times.
                  I was concerned about it possibly believing that "/dir1/file.inc" had been
                  included when it was "/dir2/file.inc". In other words, I was concerned
                  about the "once" part of it and how it remembers what has already been
                  included.
                  >
                  My question was about the "once" part of it and how that might interact
                  with paths.
                  /dir1/file.inc is different from /dir2/file.inc. You might run into problems
                  if you have a hell of symlinks and hardlinks between those directories.

                  If you use no symlinks, though, they'll be always different files: different
                  to you, different to the filesystem, different to PHP.

                  --
                  ----------------------------------
                  Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

                  Un ordenador no es un televisor ni un microondas, es una herramienta
                  compleja.

                  Comment

                  Working...