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

后端 未结 4 1281
情书的邮戳
情书的邮戳 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).

提交回复
热议问题