我在相对包含文件方面遇到了一些问题。问题不在于require_once无法找到文件,但事实是它可以在文件不在的地方找到文件。我已经把问题解决了三个小班。
所以这是我的目标结构:
public_html/
index.php
commands/
CommandFactory.php
account/
Status.php
的index.php:
require_once 'commands/CommandFactory.php';
$factory = new CommandFactory();
CommandFactory.php:
class CommandFactory{
public function __construct()
{
echo getcwd() . '<br/>'; //xxx\public_html (which is good, this is where index.php is
//require_once 'account/Status.php'; // This works as expected
require_once 'account/Status.php'; // This ALSO works, but it shouldn't
$status = new Status(); // This works, though it shouldn't.
}
}
我已经通过将CommandFactory移动到另一个dir来测试,这解决了问题。但是,我无法理解为什么可以包含该文件。它不应该包含Status,相对于CommandFactory,它应该是来自index.php的相对。我添加了当前的工作目录,看看出了什么问题,但我无法弄明白。有谁知道这是如何以及为什么有效?
答案 0 :(得分:1)
来自php.net网站:
根据给定的文件路径包含文件,如果没有给出,则包含 指定的include_path。如果文件中没有找到 include_path,include将最终签入调用脚本自己的 失败前的目录和当前工作目录。该 如果找不到文件,include构造将发出警告;这个 是来自require的不同行为,会发出致命错误