U mnie każda klasa jest w osobnym pliku. Nazwa pliku zawierającego definicję klasy lub interfejsu kończy się na .class.php lub interface.php. I tylko takie pliki są ładowany do mapy.
Wycinki kodu z mojego frameworka:
<? /** * Checks whether the specified path exists. * If the directory is not found, it'll try to create one. * * @param string $dirname * @return void */ function ensure_directory_exists($dirname) { $path = ''; foreach ($sub_dirs as $dir) { $path .= $dir . '/'; { { throw new DaeronException('Could not create a directory \"' . $path . '\"'); } } } } /** * Writes the supplied content to a file. * * @param string $filename Name of a file to write to. * @param mixed $content Data to write to a file. * @return void */ function file_write($filename, $content) { if($fp) { } } class Autoloader { /** * Map of class names and their localization. * * @var array * @static */ /** * Loads specified class. * * @param string $class_name * @return void */ { $cached_map = APP_DIR . '/var/cache/autoload.php'; { self::generateMap(); } { throw new Exception('Unknown class ' . $class_name); } require_once self::$map[$class_name]; } /** * * * */ { $cached_map = APP_DIR . '/var/cache/autoload.php'; self::scan(DAERON_DIR); self::scan(APP_DIR); $compiled = \"<?phpn\"; file_write($cached_map, $compiled); } /** * * * */ private static function scan($dir) { $match = array(); $rd = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), true); foreach ($rd as $file) { if(!is_file($file->getPathname())) continue; if (preg_match('/([a-z0-9_]+).(class|interface).php$/i', $file->getPathName(), $match)) { self::$map[strtolower($match[1])] = $file->getPathName(); } } } } function __autoload($class_name) { Autoloader::load($class_name); } ?>
Porównanie szybkości (dla stosunkowo małej liczby plików):
- czytanie mapy z generowaniem: 35,67 ms
- czytanie z cache'owanej mapy: 00,57 ms
Kod Vengeance'a:
- czytanie mapy z generowaniem: 129.35 ms
A czytanie z cache wygląda identycznie
