在PHP中:
什么时候应该使用require和include? 什么时候应该使用require_once vs. include_once?
在PHP中:
什么时候应该使用require和include? 什么时候应该使用require_once vs. include_once?
当前回答
无论何时使用require_once(),当您在当前文件中只需要被调用的文件一次时,可以在文件中使用require_once()来包含另一个文件。 在这个例子中,我有一个test1.php。
<?php
echo "today is:".date("Y-m-d");
?>
在另一个文件中,命名为test2.php
<?php
require_once('test1.php');
require_once('test1.php');
?>
当你看到m需要test1文件两次,但该文件将包含test1一次,而在第二次调用时,这将被忽略。并且不停止将显示输出一次。
当你在使用'include_once() '时,当你在当前文件中需要调用的文件不止一次时,可以在一个文件中使用'include_once() '来包含另一个文件。 在本例中,我有一个名为test3.php的文件。
<?php
echo "today is:".date("Y-m-d");
?>
在另一个文件中,命名为test4。php
<?php
include_once('test3.php');
include_once('test3.php');
?>
在您观察时,包含test3文件的m将一次包含该文件,但会停止进一步执行。
其他回答
我注意到的一件事是,当使用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();
}
需要关键部分,如授权,并包括所有其他部分。
多重包含是非常糟糕的设计,必须完全避免。所以,*_once并不重要。
这通常是一个问题,你是想有条件地加载一个客户端库,还是不管你是否要使用它都直接加载它。
这里有一个具体的例子;详述PCJ的发言。
假设你有一个配置文件存储你的数据库用户名和密码(conf.php):
<?php
//my site configuration file
//For Database
$location='localhost';
$dbuser='yourname';
$userpw='yourpassword';
$database='nameofdatabase';
?>
和一个带有使用数据库的静态函数的类:
<?php
class UsedInLoop {
public static function databaseQuery(){
require(/path/to/conf.php); //require_once will not work here
$db = new mysqli($location, $dbuser, $userpw, $database);
//yada yada yada
}
}
?>
这个静态函数在循环中被迭代调用的另一个函数中使用:
<?php
require_once('path/to/arbitraryObject.php'); //either will likely be OK at this level
$obj = new arbitraryObject();
foreach($array as $element){
$obj->myFunction();
}
?>
您只能要求/包含该类一次。如果在循环的每次迭代中都需要/包含它,则会得到一个错误。但是,每次调用静态函数时都必须包含conf文件。
<?php
class arbitraryObject {
public function myFunction(){
require_once(/path/to/UsedInLoop.php); //This must be require_once. require() will not work
UsedInLoop::databaseQuery();
}
}
?>
当然,将它移到函数之外可以解决这个问题:
<?php
require(/path/to/UsedInLoop.php); //now require() is fine
class arbitraryObject {
public function myFunction(){
UsedInLoop::databaseQuery();
}
}
?>
除非您担心加载一个可能只在某些条件下使用的类的开销,并且不想在不使用时加载它。
Include / Require你也可以包含同一个文件不止一次:
require()与include()相同,除了失败时,它也会产生致命的E_COMPILE_ERROR级别的错误。换句话说,它将停止脚本,而include()只发出一个允许脚本继续的警告(E_WARNING)。
Require_once / include_once
和include/require是一样的,除了PHP会检查文件是否已经被包含,如果已经被包含,就不再包含(require)它。
Require的开销比include大,因为它必须首先解析文件。用包含替换require通常是一种很好的优化技术。