我爱编程

PHP autoload 自动加载类

2018-04-08  本文已影响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");
上一篇 下一篇

猜你喜欢

热点阅读