【发布时间】:2013-05-06 11:11:03
【问题描述】:
抱歉,可能会重复询问。但是很难用这个容易混淆的关键字来搜索答案。
所以这里是场景:
我试图得到一个建议来制作我的简单自动加载器。这是我到目前为止所做的:
private function getAutoInclude($classfile) {
$classfileLower = strtolower($classfile);
if (isset($this->configs['Paths']['base.'.$classfileLower])) { // Use path scope to locate file first
return require_once($this->configs['Paths']['base.'.$classfileLower]['Path']);
} elseif ($this->configs['LibRoot'] && strpos($classfile, '\\') !== false) { // If above not work, use namespace to locate file
return require_once($this->configs['LibRoot'] . DIRECTORY_SEPARATOR . str_replace(array('\\', '/', '_'), DIRECTORY_SEPARATOR, ltrim($classfile, '\\')) . '.php');
}
return false;
}
到目前为止它运行良好,但唯一让我感到困惑的是,有些人告诉我必须对我包含的文件进行 file_exists 检查,以便我可以更安全、更快地包含它。
所以考虑到我想要包含的文件必须在每次包含它时都存在,在这种情况下我真的需要 file_exists 吗?
(我知道这个问题就像一个新手问的那样。但是当我听到人们说如果 file_exists 可以使文件包含更快时,它打破了我对 PHP 的一些知识。)
【问题讨论】:
-
这能回答你的问题吗? :) stackoverflow.com/questions/13705179/…
-
@Ben,这个问题实际上有两种情况:文件存在与否。矿山只有一个。而且我没有抑制错误。
-
@Ben,我对这种情况进行了另一项测试,结果是,没有 file_exists 是....平均时间更快一点。考虑到我没有压制任何东西(它只是死了),我认为没有 file_exists 会变得更快。感谢您指出该问题的链接。
标签: php include file-exists