如何在PHP中为多个目录使用spl_autoload_register
PHP多目录SPL类自动加载实现
下面是一段用于实现多路径类自动加载的代码,可帮助PHP开发者掌握多目录场景下SPL自动加载的正确用法,经梳理优化后能有效规整代码结构:
spl_autoload_register(function($class){ $paths = array( 'classes/', 'engine/' ); foreach($paths as $path){ $file = LIBRARY . $path . str_replace('\\', '/', strtolower($class)) . '.php'; if(file_exists($file)){ $files = array($file); } } foreach($files as $file){ if(is_file($file)){ include_once($file); return true; }else{ return false; } } });
内容的提问来源于stack exchange,提问作者bruno gilberto
相关产品推荐
相关产品推荐

