美文网首页我爱编程
PHP autoload 自动加载类

PHP autoload 自动加载类

作者: 晓得为_ | 来源:发表于2018-04-08 14:48 被阅读0次
function appAutoload($classname) {
    $LIB_PATHS = array(APP_PATH . "/library",APP_PATH . "/model");
    $arr = explode("_", $classname);
    $file = '';
    for ($i = 0; $i < count($arr); $i++) {
        if ($i === count($arr) - 1) {
            $file .= (ucfirst($arr [$i]) . ".php");
        } else {
            $file .= (strtolower($arr [$i]) . "/");
        }
    }

    foreach ($LIB_PATHS as $path) {
        $fileTmp = $path . '/' . $file;
        if (file_exists($fileTmp)) {
            require_once $fileTmp;
            break;
        }
    }
}

spl_autoload_register("appAutoload");

相关文章

网友评论

    本文标题:PHP autoload 自动加载类

    本文链接:https://www.haomeiwen.com/subject/lvtgkxtx.html