🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] * * * * * ## 1 自动加载器实例化源代码(thinkphp/library/think/Loader) ~~~ public static function table($name = '', array $options = []) { static $_model = []; if (strpos($name, ':')) { list($class, $name) = explode(':', $name); } else { $class = 'think\\Model'; } $guid = $name . '_' . $class; if (!isset($_model[$guid])) { $_model[$guid] = new $class($name, $options); } return $_model[$guid]; } public static function model($name = '', $layer = MODEL_LAYER) { if (empty($name)) { return new Model; } static $_model = []; if (isset($_model[$name . $layer])) { return $_model[$name . $layer]; } if (strpos($name, '/')) { list($module, $name) = explode('/', $name, 2); } else { $module = APP_MULTI_MODULE ? MODULE_NAME : ''; } $class = self::parseClass($module, $layer, $name); $name = basename($name); if (class_exists($class)) { $model = new $class($name); } else { $class = str_replace('\\' . $module . '\\', '\\' . COMMON_MODULE . '\\', $class); if (class_exists($class)) { $model = new $class($name); } else { Log::record('实例化不存在的类:' . $class, 'notice'); $model = new Model($name); } } $_model[$name . $layer] = $model; return $model; } public static function controller($name, $layer = '', $empty = '') { static $_instance = []; $layer = $layer ?: CONTROLLER_LAYER; if (isset($_instance[$name . $layer])) { return $_instance[$name . $layer]; } if (strpos($name, '/')) { list($module, $name) = explode('/', $name); } else { $module = APP_MULTI_MODULE ? MODULE_NAME : ''; } $class = self::parseClass($module, $layer, $name); if (class_exists($class)) { $action = new $class; $_instance[$name . $layer] = $action; return $action; } elseif ($empty && class_exists($emptyClass = self::parseClass($module, $layer, $empty))) { return new $emptyClass; } else { throw new Exception('class [ ' . $class . ' ] not exists', 10001); } } public static function db($config = []) { return Db::connect($config); } public static function action($url, $vars = [], $layer = CONTROLLER_LAYER) { $info = pathinfo($url); $action = $info['basename']; $module = '.' != $info['dirname'] ? $info['dirname'] : CONTROLLER_NAME; $class = self::controller($module, $layer); if ($class) { if (is_scalar($vars)) { if (strpos($vars, '=')) { parse_str($vars, $vars); } else { $vars = [$vars]; } } return App::invokeMethod([$class, $action . Config::get('action_suffix')], $vars); } } public static function instance($class, $method = '') { static $_instance = []; $identify = $class . $method; if (!isset($_instance[$identify])) { if (class_exists($class)) { $o = new $class(); if (!empty($method) && method_exists($o, $method)) { $_instance[$identify] = call_user_func_array([ & $o, $method], []); } else { $_instance[$identify] = $o; } } else { throw new Exception('class not exist :' . $class, 10007); } } return $_instance[$identify]; } public static function parseName($name, $type = 0) { if ($type) { return ucfirst(preg_replace_callback('/_([a-zA-Z])/', function ($match) {return strtoupper($match[1]);}, $name)); } else { return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_")); } } public static function parseClass($module, $layer, $name) { $name = str_replace(['/', '.'], '\\', $name); $array = explode('\\', $name); $class = self::parseName(array_pop($array), 1); $path = $array ? implode('\\', $array) . '\\' : ''; return APP_NAMESPACE . '\\' . (APP_MULTI_MODULE ? $module . '\\' : '') . $layer . '\\' . $path . $class; } ~~~ * * * * * ## 2 分析 1 table($name='',arrat $option) 实例化以$name为名字的Model对象 `public static function table($name = '', array $options = []){}` 创建一个静态变量数组$_model缓存所有实例化的Model对象。 * * * * * 2 model($name='',$layer) 实例化以$name为名字的Model对象 `public static function model($name = '', $layer = MODEL_LAYER){}` 同上 使用静态变量数组$_model缓存 对比table()与model() table()创建无模型文件的的Model。直接对应数据表 model()创建有模型文件关联的Model。对应模型的子类 * * * * * 3 controller() 实例化以$name为名字的控制器 `public static function controller($name, $layer = '', $empty = ''){}` 创建一个静态变量数组$_instance缓存所有实例化的控制对象 * * * * * 4 db($config=[]) 根据配置创建数据库连接对象。 `public static function db($config = []){}` * * * * * 5action() 远程调用url对应的控制器方法 `public static function action($url, $vars = [], $layer = CONTROLLER_LAYER){}` 使用self::controller()创建url对应控制器 使用App::invokeMethod()调用对应控制的action方法 * * * * * 6 instance() 创建$class对应的类,如果存在$method,则调用对象的$method `public static function instance($class, $method = ''){}` 使用$_instance 缓存创建的对象 * * * * * 7parseName() 转换字符串命名风格 `public static function parseName($name, $type = 0){}` * * * * * 8 parseClass() 解析类名到系统中命名空间模块分层下的目录类名文件 `public static function parseClass($module, $layer, $name){}` * * * * * * * * * * ## 3 使用方法 TODO ## 4 总结 自动加载器的实例化方法用来实例化对象并缓存。