From fd6aaa42f9cb52424e3061941df42f39e35cafb7 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 18 Dec 2015 22:35:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20traits\think\instance=20?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E5=AE=9E=E4=BE=8B=E5=8C=96=E5=92=8C=E9=9D=99?= =?UTF-8?q?=E6=80=81=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/traits/think/instance.php | 40 +++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 library/traits/think/instance.php diff --git a/library/traits/think/instance.php b/library/traits/think/instance.php new file mode 100644 index 00000000..a32fb462 --- /dev/null +++ b/library/traits/think/instance.php @@ -0,0 +1,40 @@ + +// +---------------------------------------------------------------------- + +namespace traits\think; + +trait Instance +{ + protected static $instance = null; + + // 实例化(单例) + public static function instance($options = []) + { + if (is_null(self::$instance)) { + self::$instance = new self($options); + } + return self::$instance; + } + + // 静态调用 + public static function __callStatic($method, $params) + { + if (is_null(self::$instance)) { + self::$instance = new self(); + } + $call = substr($method, 1); + if (0 === strpos($method, '_') && is_callable([self::$instance, $call])) { + return call_user_func_array([self::$instance, $call], $params); + } else { + throw new \think\Exception("not exists method:" . $method); + } + } +}