donglan9651
2013-05-12 06:21
浏览 46
已采纳

试图包含文件,file_exists是必不可少的?

Sorry for possiblely reduplicate asking. But it's hard to use this confusable keyword to search a answer.

So here is scenario:

I try to get a advise to make my simple autoloader. Here is i made so far:

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;
}

It works well so far but only thing confused me is, some people tells me i must to do a file_exists check on the file i'm including so i can include it more safer AND more faster.

So consider the file i want to included must be there each time i include it, will i really have to file_exists in this scenario or not?

(I know this question just like a newbie asked. But when i heard people say if file_exists can make file include faster, it break some of my knowledge on PHP.)

图片转代码服务由CSDN问答提供 功能建议

很抱歉可能会重复询问。 但是很难用这个可混淆的关键字来搜索答案。

所以这是场景:

我试着建议让我变得简单 自动加载磁带机。 这是我到目前为止:

 私有函数getAutoInclude($ classfile){
 $ classfileLower = strtolower($ classfile); 
 
 if(isset($ this)  - > configs ['Paths'] ['base。'。$ classfileLower])){//使用路径范围首先找到文件
返回require_once($ this-> configs ['Paths'] ['base。  '。$ classfileLower] ['Path']); 
} elseif($ this-> configs ['LibRoot']&& strpos($ classfile,'\\')!== false){// 如果以上不起作用,请使用命名空间来定位文件
返回require_once($ this-> configs ['LibRoot']。DIRECTORY_SEPARATOR.str_replace(array('\\','/','_'),DIRECTORY_SEPARATOR,ltrim  ($ classfile,'\\'))。'。php'); 
} 
 
返回false; 
} 
   
 
 

效果很好 到目前为止,但只有我困惑的是,有些人告诉我,我必须做一个file_exists检查我包含的文件,所以我可以包括它更安全,更快。

因此,考虑我想要包含的文件必须在每次包含它时,我是否真的必须在这种情况下使用file_exists?

(我知道这个问题就像新手问的那样。但是当我听到人们说file_exists可以让文件包含更快时,它会破坏我对PHP的一些了解。)

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doudou7361 2013-05-12 06:23
    已采纳

    The file you are including is required. (Presumably anyway... you are using require_once().) If the file isn't there, your script will fail. No need for all this checking and nonsense, just do the require_once() call.

    Also, add your paths to the include path. Let PHP figure it out. No need to write code to search for a file.

    已采纳该答案
    打赏 评论
  • dthok9648 2013-05-12 06:34

    just use the __autoload() function and save yourself the stress because if those files you're requiring are nonexistence then you're i trouble of getting those red table error report

    打赏 评论

相关推荐 更多相似问题