【发布时间】:2019-03-07 15:09:13
【问题描述】:
根据 PHP 的文档,我预计以下文件层次结构
.
├── autoload.php
└── etc
└── php-lib
├── autoload.php
└── common.php
2 directories, 3 files
及以下内容:
cat autoload.php
<?php
echo "I am the wrong autoloader";
cat etc/php-lib/autoload.php
<?php
echo "This is the right one!";
cat etc/php-lib/common.php
<?php
include_once('autoload.php');
当我跑步时
$ php etc/php-lib/common.php
我会得到以下输出:
This is the right one!
我希望这是因为手册指出:
Files are included based on the file path given or, if none is given, the
include_path specified. If the file isn't found in the include_path,
include will finally check in the calling script's own directory and the
current working directory before failing. The include construct will emit
a warning if it cannot find a file; this is different behavior from
require, which will emit a fatal error.
但是,我明白了:
I am the wrong autoloader
那么为什么加载的是autoload.php 而不是etc/php-lib/autoload.php?
事实上,当我 strace 时,包含顺序似乎比手册预期的更奇怪:
第一种情况,上面描述的所有文件都存在
getcwd("/home/hvdb/temp2", 4096) = 17
lstat("/home/hvdb/temp2/./autoload.php", {st_mode=S_IFREG|0644, st_size=40, ...}) = 0
lstat("/home/hvdb/temp2/autoload.php", {st_mode=S_IFREG|0644, st_size=40, ...}) = 0
openat(AT_FDCWD, "/home/hvdb/temp2/autoload.php", O_RDONLY) = 3
我们看到 PHP 首先在 cwd 中查找 autoload.php。
如果我删除该文件,即./autoload.php,我会得到以下信息:
getcwd("/home/hvdb/temp2", 4096) = 17
lstat("/home/hvdb/temp2/./autoload.php", 0x7fff7f04e880) = -1 ENOENT (No such file or directory)
lstat("/usr/share/php/autoload.php", 0x7fff7f04e880) = -1 ENOENT (No such file or directory)
lstat("/home/hvdb/temp2/etc/php-lib/autoload.php", {st_mode=S_IFREG|0644, st_size=38, ...}) = 0
只有这样我才能看到 PHP(仍然首先检查 cwd 中的 autoload.php)在 include_directories 中设置的目录,然后在调用脚本的目录中看到 autoloader.php。
那么,是文档有误,还是我误解了手册?
【问题讨论】:
-
我猜你的 include_path 以
.开头 -
echo get_include_path()显示什么? -
或
php -d include_path='' etc/php-lib/common.php -
文档的相关部分是
Files are included based on [...] the include_path specified -
@ChrisLear 是对的。很可能您已经在 php.ini 文件中设置了
include_path。我自己在这里测试了你的代码,我得到了正确的代码,没有任何问题。