Connecting Tech Pros Worldwide Help | Site Map

require_once() and Subdirectories

David T. Ashley
Guest
 
Posts: n/a
#1: Jun 7 '07
Hi,

Does require_once() treat "file.inc" and "subdirectory/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?=
Guest
 
Posts: n/a
#2: Jun 7 '07

re: require_once() and Subdirectories


David T. Ashley wrote:
Quote:
Does require_once() treat "file.inc" and "subdirectory/file.inc" as the
same things or different?
Different, of course.
Quote:
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.
Rik
Guest
 
Posts: n/a
#3: Jun 7 '07

re: require_once() and Subdirectories


On Thu, 07 Jun 2007 16:56:34 +0200, David T. Ashley <dta@e3ft.comwrote:
Quote:
Hi,
>
Does require_once() treat "file.inc" and "subdirectory/file.inc" as the
same
things or different?
Different, obviously, it searches another directory for the file instead
of the current working directory.
Quote:
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
phpCodeHead
Guest
 
Posts: n/a
#4: Jun 7 '07

re: require_once() and Subdirectories


On Jun 7, 9:56 am, "David T. Ashley" <d...@e3ft.comwrote:
Quote:
Hi,
>
Does require_once() treat "file.inc" and "subdirectory/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.

http://us.php.net/manual/en/function.include.php

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

David T. Ashley
Guest
 
Posts: n/a
#5: Jun 7 '07

re: require_once() and Subdirectories


"Iván Sánchez Ortega" <ivansanchez-alg@rroba-escomposlinux.-.punto.-.org>
wrote in message news:f4979g$8j9$1@hercules.cohp1...
Quote:
David T. Ashley wrote:
>
Quote:
>Does require_once() treat "file.inc" and "subdirectory/file.inc" as the
>same things or different?
>
Different, of course.
>
Quote:
>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.


David T. Ashley
Guest
 
Posts: n/a
#6: Jun 7 '07

re: require_once() and Subdirectories


"phpCodeHead" <phpcodehead@gmail.comwrote in message
news:1181229551.203483.302990@p77g2000hsh.googlegr oups.com...
Quote:
On Jun 7, 9:56 am, "David T. Ashley" <d...@e3ft.comwrote:
Quote:
>Hi,
>>
>Does require_once() treat "file.inc" and "subdirectory/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.
>
http://us.php.net/manual/en/function.include.php
>
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.


David T. Ashley
Guest
 
Posts: n/a
#7: Jun 7 '07

re: require_once() and Subdirectories


"Rik" <luiheidsgoeroe@hotmail.comwrote in message
news:op.ttj4mwavqnv3q9@metallium...
Quote:
On Thu, 07 Jun 2007 16:56:34 +0200, David T. Ashley <dta@e3ft.comwrote:
>
Quote:
>Hi,
>>
>Does require_once() treat "file.inc" and "subdirectory/file.inc" as the
>same
>things or different?
>
Different, obviously, it searches another directory for the file instead
of the current working directory.
>
Quote:
>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.


Rik
Guest
 
Posts: n/a
#8: Jun 7 '07

re: require_once() and Subdirectories


On Thu, 07 Jun 2007 17:45:49 +0200, David T. Ashley <dta@e3ft.comwrote:
Quote:
Quote:
Quote:
>>Does require_once() treat "file.inc" and "subdirectory/file.inc" as the
>>same
>>things or different?
>>
>Different, obviously, it searches another directory for the file instead
>of the current working directory.
>>
Quote:
>>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.
No, it wasn't.
Quote:
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_files(), you'd see PHP stores the full path of all
files, not just the filename.
--
Rik Wasmus
=?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=
Guest
 
Posts: n/a
#9: Jun 7 '07

re: require_once() and Subdirectories


David T. Ashley wrote:
Quote:
Thanks for your reply.
Tip: you *don't* have to post the same message THREE times.
Quote:
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.
Closed Thread