From faeef16f58ea42bc419ff3c1d0b3925c89f45a5d Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 16 Jun 2016 12:24:58 +0800 Subject: [PATCH] =?UTF-8?q?Loader=E7=B1=BB=E6=94=B9=E8=BF=9B=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0clearInstance=E6=96=B9=E6=B3=95=20=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E6=B8=85=E9=99=A4=E5=AE=9E=E4=BE=8B=E5=8C=96=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E5=8D=95=E4=BE=8B=E5=AD=98=E5=82=A8=20=E5=8E=BB=E9=99=A4=20ins?= =?UTF-8?q?tance=E6=96=B9=E6=B3=95=20App=E7=B1=BBrun=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B8=85=E9=99=A4=E5=AE=9E=E4=BE=8B=E5=8C=96?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/App.php | 4 ++- library/think/Loader.php | 60 ++++++++++++---------------------------- 2 files changed, 20 insertions(+), 44 deletions(-) diff --git a/library/think/App.php b/library/think/App.php index 61ef4808..9a20883c 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -124,7 +124,9 @@ class App // 监听app_end Hook::listen('app_end', $data); - + // 清空类的实例化 + Loader::clearInstance(); + // 输出数据到客户端 if ($data instanceof Response) { return $data; diff --git a/library/think/Loader.php b/library/think/Loader.php index 0ec6a948..df5b3cff 100644 --- a/library/think/Loader.php +++ b/library/think/Loader.php @@ -18,6 +18,7 @@ use think\Request; class Loader { + protected static $instance = []; // 类名映射 protected static $map = []; // 加载列表 @@ -275,9 +276,8 @@ class Loader */ public static function model($name = '', $layer = 'model', $appendSuffix = false, $common = 'common') { - static $_model = []; - if (isset($_model[$name . $layer])) { - return $_model[$name . $layer]; + if (isset(self::$instance[$name . $layer])) { + return self::$instance[$name . $layer]; } if (strpos($name, '/')) { list($module, $name) = explode('/', $name, 2); @@ -295,7 +295,7 @@ class Loader throw new ClassNotFoundException('class [ ' . $class . ' ] not exists', $class); } } - $_model[$name . $layer] = $model; + self::$instance[$name . $layer] = $model; return $model; } @@ -310,11 +310,6 @@ class Loader */ public static function controller($name, $layer = 'controller', $appendSuffix = false, $empty = '') { - static $_instance = []; - - if (isset($_instance[$name . $layer])) { - return $_instance[$name . $layer]; - } if (strpos($name, '/')) { list($module, $name) = explode('/', $name); } else { @@ -322,9 +317,7 @@ class Loader } $class = self::parseClass($module, $layer, $name, $appendSuffix); if (class_exists($class)) { - $action = new $class(Request::instance()); - $_instance[$name . $layer] = $action; - return $action; + return new $class(Request::instance()); } elseif ($empty && class_exists($emptyClass = self::parseClass($module, $layer, $empty, $appendSuffix))) { return new $emptyClass(Request::instance()); } else { @@ -347,10 +340,9 @@ class Loader if (empty($name)) { return new Validate; } - static $_instance = []; - if (isset($_instance[$name . $layer])) { - return $_instance[$name . $layer]; + if (isset(self::$instance[$name . $layer])) { + return self::$instance[$name . $layer]; } if (strpos($name, '/')) { list($module, $name) = explode('/', $name); @@ -368,7 +360,7 @@ class Loader throw new ClassNotFoundException('class [ ' . $class . ' ] not exists', $class); } } - $_instance[$name . $layer] = $validate; + self::$instance[$name . $layer] = $validate; return $validate; } @@ -407,33 +399,6 @@ class Loader return App::invokeMethod([$class, $action . Config::get('action_suffix')], $vars); } } - /** - * 取得对象实例 支持调用类的静态方法 - * - * @param string $class 对象类名 - * @param string $method 类的静态方法名 - * - * @return mixed - * @throws ClassNotFoundException - */ - 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 ClassNotFoundException('class not exist :' . $class, $class); - } - } - return $_instance[$identify]; - } /** * 字符串命名风格转换 @@ -466,4 +431,13 @@ class Loader $path = $array ? implode('\\', $array) . '\\' : ''; return App::$namespace . '\\' . ($module ? $module . '\\' : '') . $layer . '\\' . $path . $class; } + + /** + * 初始化类的实例 + * @return void + */ + public static function clearInstance() + { + self::$instance = []; + } }