什么是php';包括订单吗?

什么是php';包括订单吗?,php,include,Php,Include,基于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 ech

基于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');
我将得到以下输出:

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

事实上,当我回顾这一点时,根据手册,包含顺序似乎比预期的更奇怪:

第一种情况,上面描述的所有文件都存在

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
只有这样,我才能在include_目录中设置的目录中看到PHP(仍然首先检查cwd中的
autoload.PHP
),然后在调用脚本的目录中看到
autoloader.PHP


那么,是文档错误,还是我误解了手册?

从评论判断,问题是您已将php.ini文件中的
include_path设置为默认设置。在您的情况下

这就是为什么在脚本中最好有一个配置文件,您可以在其中创建如下常量:

PHP>=5.3.0

define("PATH", __DIR__);
PHP<5.3.0

define("PATH", dirname(__FILE__));

这将使您可以控制要从当前工作目录中包含的文件,并确保脚本仍能在不同的环境中工作,您可能无法控制PHP的配置。

您的include_路径以
开始,我猜
echo get_include_path()是什么
show?或
php-d include_path=''etc/php lib/common.php
文档的相关部分是
文件是基于[…]指定的include_路径包含的
@ChrisLear是正确的。很可能您已经在php.ini文件中设置了
include_path
。我自己在这里测试了你的代码,我得到了正确的返回,没有任何问题。你的最后一段并不像OP评论中讨论的那样真的是一个替代品。请删除,这样我才能接受这个答案。@hbogert删除了它。尽管我相信这对于将来访问这个问题的其他用户来说是一个很好的信息:)假设所有include/require语句都应该由composer或PSR-4之类的东西来完成。这要么相当固执己见,要么根本不适用。
define("PATH", dirname(__FILE__));