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

后端 未结 4 1279
情书的邮戳
情书的邮戳 2020-12-08 03:27

Lots of famous PHP scripts including WordPress use dirname(__FILE__).\'/myParent.php\' instead of just \'myParent.php\' when including files in the

4条回答
  •  执笔经年
    2020-12-08 03:50

    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.

    * Benchmark of include using a path that requires include_path lookup versus a relative path that does not. Tested over 100000 iterations each

    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.

    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)

提交回复
热议问题