0
オートロードの方が高速ですか?カスタムオートロードとデフォルトの比較
private $_directoriesToLook = array("framework/", "models/", "controllers/");
カスタムオートローダ:
private function customAutoloader($class_name)
{
if(class_exists($class_name))
return true;
foreach($this->_directoriesToLook as $directory)
{
$files = scandir($directory);
if(in_array($class_name.".php", $files))
{
require_once($directory.$class_name.".php");
return true;
}
}
return false;
}
spl_autoload_register(array($this, "customAutoloader"));
デフォルトオートローダ:
set_include_path(get_include_path().PATH_SEPARATOR.implode(PATH_SEPARATOR, $this->_directoriesToLook));
spl_autoload_extensions(".php");
spl_autoload_register();
私は、デフォルトの1が速くなければならないことを読んだが、私のテストによるとカスタムメソッドが勝ちます。 デフォルトの方法の欠点は、クラスがすべて小文字でなければならないことです。
デフォルトの方法の問題は、ラクダのケースクラス/ファイル名を使用できないことです。 – Acute
私は、デフォルトのオートローダーからのわずかなパフォーマンス向上を気にしません。キャメルケースをサポートしているカスタムオートローダーを実行してください。読み込み可能なコードは、実行時間が数msかかることよりも重要です。 – Basti