很抱歉可能会重复询问。但是很难使用这个易混淆的关键字来搜索答案。
所以这里是场景:
我试图得到一个建议来制作我的简单自动加载器。这是我到目前为止所做的:
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 的一些知识。)