PHP 5.3 開始支援 __DIR__,從測試中可以知道 __DIR__ 略快于 dirname(__FILE__)。
function calc($t0, $t1) { $v0 = array_sum(explode(' ', $t0)); $v1 = array_sum(explode(' ', $t1)); $sub = $v1 - $v0; echo "$v1 - $v0 = $sub\n"; return $sub; } $t0 = microtime(); for ($i = 0; $i < 1E6; $i++) require_once __DIR__.'/a.php'; $t1 = microtime(); $new = calc($t0, $t1); // output: 1420769613.0535 - 1420769612.1561 = 0.89736700057983 $t0 = microtime(); for ($i = 0; $i < 1E6; $i++) require_once dirname(__FILE__).'/a.php'; $t1 = microtime(); $old = calc($t0, $t1); // output: 1420769614.9498 - 1420769613.0548 = 1.8950188159943 echo ($old - $new) / $old; // output: 0.52646011057731
參考資料:http://www.php.net/manual/en/language.constants.predefined.php
PHP 5.3.24 (cli) (built: Jun 10 2013 16:42:20) Copyright (c) 1997-2013 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2013 Zend Technologies with Xdebug v2.2.0rc1, Copyright (c) 2002-2012, by Derick Rethans