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.)

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证