有什么比 require('myParent.php') 更好的 require(dirname(__FILE__).'/'.'myParent.php') 呢?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2220443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:35:22  来源:igfitidea点击:

What's better of require(dirname(__FILE__).'/'.'myParent.php') than just require('myParent.php')?

phpincluderequire

提问by datasn.io

Lots of famous PHP scripts including WordPress use dirname(__FILE__).'/myParent.php'instead of just 'myParent.php'when including files in the same directory of the currently running script.

许多著名的 PHP 脚本(包括 WordPress)都在使用,dirname(__FILE__).'/myParent.php'而不仅仅是'myParent.php'将文件包含在当前运行的脚本的同一目录中。

Aren't they the same thing? Why do you prefer typing more?

他们不是一回事吗?为什么你更喜欢打字?

Thanks.

谢谢。

回答by Gumbo

PHP needs to know the absolute path to the file. dirname(__FILE__).'/myParent.php'already is the absolute path but 'myParent.php'requires a lookup using the given paths in include_pathto get an absolute path and find the file. A better choice would be './myParent.php':

PHP 需要知道文件的绝对路径。dirname(__FILE__).'/myParent.php'已经是绝对路径,但'myParent.php'需要使用include_path 中的给定路径进行查找以获取绝对路径并找到文件。更好的选择是'./myParent.php'

However, it is more efficient to explicitly use include './file'than having PHP always check the current directory for every include.

然而,显式使用include './file'比让 PHP 总是检查当前目录中的每个包含更有效。

回答by Justin Johnson

Besides the performance increase (which is likely a pre-optimization in most cases*), it also protects from the (very odd) scenario where the environment's PHP configuration does not have the current directory (.) as part of the include path.

除了性能提升(在大多数情况下这可能是预先优化*),它还可以防止环境的 PHP 配置没有将当前目录 ( .) 作为包含路径的一部分的(非常奇怪的)场景。

* Benchmark of includeusing a path that requires include_pathlookup versus a relative path that does not. Tested over 100000 iterations each

*include使用需要include_path查找的路径与不需要查找的相对路径的基准。每次测试超过 100000 次迭代

Results

结果

include("include.php"):   8.3664200305939s
include("./include.php"): 8.3511519432068s

(8.3664200305939 - 8.3511519432068) / 100000 = 0.000000152680874s

Unless you're including hundreds or thousands of files, 0.0000001s is negligible at best.

除非您包含数百或数千个文件,否则 0.0000001s 充其量可以忽略不计。

Test code

测试代码

define("MAX", 100000);

ob_start();
$i = MAX;
$_t = microtime(true);
do {
    include("include.php");
} while ( --$i );
$_t = microtime(true) - $_t;
ob_end_clean();

echo "include(\"include.php\"):  {$_t}s\n";

ob_start();
$i = MAX;
$_t = microtime(true);
do {
    include("./include.php");
} while ( --$i );
$_t = microtime(true) - $_t;
ob_end_clean();

Test was conducted on a 2.16GHz Macbook 10.5.8 with PHP Version 5.2.9 (www.entropy.ch Release 7)

测试在 2.16GHz Macbook 10.5.8 和 PHP 版本 5.2.9 (www.entropy.ch Release 7) 上进行

回答by alexef

Using dirname + file name is slightly faster, because PHP will not iterate through include_pathsearching for the file. If speed matters, you will likely type more.

使用 dirname + file name 稍微快一点,因为 PHP 不会遍历include_path搜索文件。如果速度很重要,您可能会输入更多。

回答by gaborous

An added note about include('./file.php').

关于 include('./file.php') 的附加说明。

If only speed matters, then yes you can use include('./file.php'), but if you want to resolve dependencies and relative paths issues, you're better off using dirname(__ FILE __), because

如果只是速度很重要,那么是的,您可以使用 include('./file.php'),但是如果您想解决依赖项和相对路径问题,最好使用 dirname(__ FILE __),因为

include('./file.php')

will still construct paths relative to the executing script (the includingscript), while

仍然会构建相对于执行脚本(包括脚本)的路径,而

include(dirname(__FILE__).'/file.php');

will resolve paths relative to the current script where this line resides (the includedscript).

将解析相对于该行所在的当前脚本(包含的脚本)的路径。

Generally, you're better off using dirname(__ FILE __ ), since './' only gives a negligible performance increase while dirname(__ FILE __ ) gives you a lot more reliable include.

通常,您最好使用 dirname(__ FILE __ ),因为 './' 只会带来微不足道的性能提升,而 dirname(__ FILE __ ) 为您提供了更可靠的包含。

/EDIT: Also note that the benchmark done above only concerns include('./something.php'), which indeed is faster than include('something.php')because you don't have the include_path walking, but when you use dirname(__FILE__)you get the dirname()function call overhead, which makes it slower than walking the include_path (unless you have a lot paths in your include_path).

/编辑:另请注意,上面完成的基准测试只涉及include('./something.php'),这确实比include('something.php')因为您没有 include_path 行走要快,但是当您使用时,您dirname(__FILE__)会得到dirname()函数调用开销,这使得它比行走 include_path 慢(除非您您的 include_path 中有很多路径)。