在PHP中:

什么时候应该使用require和include? 什么时候应该使用require_once vs. include_once?


当前回答

要求生成致命错误,停止下一行执行,而没有找到文件。

包括生成警告,但没有停止下一行执行,而没有找到文件。

Require_once do与require do相同,但它将检查文件是否已经加载或是否要执行。

Include_once do与include do相同,但它将检查文件是否已经加载或是否要执行。

注意:include_once或require_once可能用于在特定脚本执行期间包含同一个文件并对其进行多次计算的情况,因此在这种情况下,它可能有助于避免诸如函数重新定义、变量值重新赋值等问题。

其他回答

1 -如果文件不是,"require"和"require_once"会抛出致命错误 存在并停止脚本执行

2 - "include"和"include_once"抛出警告并执行 仍在继续

3 - "require_once"和"include_once",顾名思义, 如果文件已经包含,则将不包含该文件 "require", "require_once", "include"或"include_once"

对于可重用的PHP模板,使用“include”。使用“require”表示必需的库。

“*_once”很好,因为它检查文件是否已经加载,但它只在“require_once”中对我有意义。

我的建议是99.9%的情况下只使用require_once。

而使用require或include则意味着你的代码在其他地方是不可重用的,也就是说,你正在导入的脚本实际上是在执行代码,而不是提供一个类或一些函数库。

如果你需要/包含当场执行的代码,那就是过程代码,你需要了解一种新的范式。比如面向对象编程,基于函数的编程,或者函数式编程。

如果您已经在进行面向对象或函数式编程,那么使用include_once主要会延迟在堆栈中发现错误/错误的地方。您是否希望在稍后调用do_cool_stuff()函数时知道它不可用,或者在通过要求库而期望它可用的时候知道它不可用?通常,最好立即知道您需要和期望的东西是否不可用,因此只需使用require_once。

或者,在现代OOP中,只需在使用时自动加载你的类。

我注意到的一件事是,当使用include时,我只能从包含它的文件中访问包含的文件函数。使用require_once,我可以在第二个required_once文件中运行该函数。

我建议添加

if(file_exists($RequiredFile)){
    require_once($RequiredFile);
}else{
  die('Error: File Does Not Exist');
}

因为当require_once杀死页面时,它有时会返回你的网站文件目录

下面是我做的一个自定义函数来要求文件:

function addFile($file, $type = 'php', $important=false){
    //site-content is a directory where I store all the files that I plan to require_once
    //the site-content directory has "deny from all" in its .htaccess file to block direct connections
    if($type && file_exists('site-content/'.$file.'.'.$type) && !is_dir('site-content/'.$file.'.'.$type)){
        //!is_dir checks that the file is not a folder
        require_once('site-content/'.$file.'.'.$type);
        return 'site-content/'.$file.'.'.$type;
    }else if(!$type && file_exists('site-content/'.$file) && !is_dir('site-content/'.$file)){
        //if you set "$type=false" you can add the file type (.php, .ect) to the end of the "$file" (useful for requiring files named after changing vars)
        require_once('site-content/'.$file);
        return 'site-content/'.$file;
    }else if($important){
        //if you set $important to true, the function will kill the page (which also prevents accidentally echoing the main directory path of the server)
        die('Server Error: Files Missing');
        return false;
    }else{
        //the function returns false if the file does not exist, so you can check if your functions were successfully added
        return false;
    }
}

使用的例子:

$success = addFile('functions/common');

if($success){
    commonFunction();
}else{
    fallbackFunction();
}

 

什么时候应该使用require或include? require和include函数执行相同的任务,即包含并计算指定的文件,但不同的是,当指定的文件位置无效或任何错误时,require将导致致命错误,而include将生成警告并继续代码执行。 因此,当您试图包含的文件是系统的核心,可能会对其余代码造成巨大影响时,您可以使用require函数;当您试图包含的文件是一个包含一些不太重要的代码的简单文件时,您可以使用include函数。 我个人的建议(对于不太重要的代码)是在开发阶段在代码中到处使用require函数,这样你就可以调试代码,然后在将其转移到生产环境之前用include函数替换所有的require函数,这样即使你遗漏了任何错误,也不会影响最终用户,其余的代码也能正常执行…… 什么时候应该使用require_once或require? require和require_once之间的基本区别是require_once将检查文件是否已经包含,如果已经包含,则不包含该文件,而require函数将包括该文件,而不管文件是否已经包含。 因此,当你想要一次又一次地包含某段代码时,使用require函数,而如果你只想在代码中包含某段代码一次,则使用require_once。