Relative includes and include_path
| From: | Jasper Horn | Date: | Thu, 22 Dec 2011 17:33:18 +0000 |
| Subject: | Relative includes and include_path | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-316086@lists.php.net to get a copy of this message | ||
Hi all,
On the the include_path php.net says:
"Using a . in the include path allows for relative includes as it
means the current directory. However, it is more efficient to
explicitly use include './file' than having PHP always check the
current directory for every include. "
(http://www.php.net/manual/en/ini.core.php#ini.include-path)
While this does not state that starting every include with "./" is
equivalent to having an include_path that is ".", it does suggest
exactly that.
However, I tried this in the field, and came to a different
conclusion. (I included my experiment below.)
Is the idea that those two are equivalent wrong?
Am I doing something wrong?
Is something strange going on?
Can anyone clear this issue up for me?
Thanks,
Jasper
---
The experiment:
On a machine where the include_path is ".", I had the following file structure:
- A.php
- B.php
- C.php
- file.php
- sub/includeA.php
- sub/includeB.php
- sub/includeC.php
The content of the files:
A.php
<?php
include 'file.php'
?>
B.php
<?php
include './file.php'
?>
C.php
<?php
include './../file.php'
?>
file.php
<?php
echo "Included successfully!";
?>
includeA.php
<?php
include '../A.php'
?>
includeB.php
<?php
include '../B.php'
?>
includeC.php
<?php
include '../C.php'
?>
Now if you visit A.php or B.php the file will be included
successfully. Obviously, C.php fails to include anything.
includeA.php works all the same, but includeB.php can't find file.php.
includeC.php, on the other hand, finds it just fine.
This would suggest that include_path being "." means you can include
from the path of the current file, while starting your "./" means you
start looking from the current parh.