包括被忽略的路径

Include path being ignored

我不明白这是怎么回事。我有以下内容:

set_include_path(
    get_include_path()
    .PATH_SEPARATOR.'/home/mike/www/Zend/library/'
);

似乎正确设置了值:

var_dump(get_include_path());
// string(61) ".:/usr/share/php:/usr/share/pear:/home/mike/www/Zend/library/"

这个 应该 工作,但是它会产生 Fatal error: Class 'Zend\Loader\StandardAutoloader' not found:

require_once 'Zend/Loader/StandardAutoloader.php';

但如果我使用完整路径,它工作正常:

require_once '/home/mike/www/Zend/library/Zend/Loader/StandardAutoloader.php';

知道为什么在这种情况下不使用包含路径吗?

来自Include documentation

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.

如果您使用 Zend/Loader/StandardAutoloader.php,则假定文件路径已给出,因此它使用的路径当然不会指向正确的目录。如果您使用 StandardAutoloader.php,那么它会使用 include_path 找到这个。

来自include manual

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.

然后我注意到脚本在 require 期间没有发出致命错误,而是在下一行导致错误:

$loader = new Zend\Loader\StandardAutoloader($config);

因此 require 行包含 something,只是那个 something 不包含 Zend\Loader\StandardAutoloader class.

然后我编写了这个小脚本以查看它的来源:

$include_paths = explode(PATH_SEPARATOR, get_include_path());
foreach ($include_paths as $path) {
    $file = 'Zend/Loader/StandardAutoloader.php';
    echo $path .'/'. $file;
    echo '...';
    echo (file_exists($path . '/'. $file)) ? '<b>exists</b>' : 'doesnt exist';
    echo '<br>';
}

这是结果:

./Zend/Loader/StandardAutoloader.php...doesnt exist
/usr/share/php/Zend/Loader/StandardAutoloader.php...exists
/usr/share/pear/Zend/Loader/StandardAutoloader.php...doesnt exist
/home/mike/www/Zend/library/Zend/Loader/StandardAutoloader.php...exists

结果我有两个文件 与我试图包含的内容相匹配。当我打开 /usr/share/php/Zend/Loader/StandardAutoloader.php 时,我可以看到它使用没有名称空间的 class Zend_Loader_StandardAutoloader。这是来自 Zend 框架 1.12.9,但我的应用程序使用的是 2.2 版。