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

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答