PHP自動匯入類
阿新 • • 發佈:2019-01-01
spl_autoload_register(function ($class) { $classNames = explode('\\',$class); if (count($classNames)>1) { switch ($classNames[0]) { case 'core': $prefix = $classNames[0] . '\\'; $base_dir = __DIR__ . '\\..\\core\\'; break; case 'app': $prefix = 'app\\'; $base_dir = __DIR__ . '\\..\\App\\'; break; } } $len = strlen($prefix); $relative_class = substr($class, $len); $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php'; if (file_exists($file)) { require $file; } });