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

后端 未结 4 1271
情书的邮戳
情书的邮戳 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:37

    An added note about 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')
    

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

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

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

    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.

    /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).

    0 讨论(0)
  • 2020-12-08 03:47

    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_path to get an absolute path and find the file. A better choice would be './myParent.php':

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

    0 讨论(0)
  • 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)

    0 讨论(0)
  • 2020-12-08 04:03

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

    0 讨论(0)
提交回复
热议问题