PHP autoload 自动加载类

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");
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 什么是__autoload? php是脚本语言,不同于c++只需要编译一次,php每次执行过程中都需要编译,所以提...
    changxiaonan阅读 414评论 1 0
  • 在PHP开发过程中,如果希望从外部引入一个class,通常会使用include和require方法,去把定义这...
    四月不见阅读 1,101评论 0 0
  • 使用 spl_autoload_register() 函数实现类的自动加载 一个文件中,往往需要使用多个别的类。这...
    xiaojianxu阅读 537评论 0 0
  • 类的自动加载是指,在外面的页面中,并不需要去“引入”类文件,但是程序会在需要的时候动态加载需要的类文件。 方法1:...
    kangyiii阅读 596评论 0 2
  • 版本 PHP5.1.2 之前使用 __autoload() 。PHP5.1.2 之后使用 spl_autoload...
    新亮笔记阅读 326评论 0 2