You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 11:46:08