Instance.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace traits\think;
  12. use think\Exception;
  13. trait Instance
  14. {
  15. protected static $instance = null;
  16. /**
  17. * @param array $options
  18. * @return static
  19. */
  20. public static function instance($options = [])
  21. {
  22. if (is_null(self::$instance)) {
  23. self::$instance = new self($options);
  24. }
  25. return self::$instance;
  26. }
  27. // 静态调用
  28. public static function __callStatic($method, $params)
  29. {
  30. if (is_null(self::$instance)) {
  31. self::$instance = new self();
  32. }
  33. $call = substr($method, 1);
  34. if (0 === strpos($method, '_') && is_callable([self::$instance, $call])) {
  35. return call_user_func_array([self::$instance, $call], $params);
  36. } else {
  37. throw new Exception("method not exists:" . $method);
  38. }
  39. }
  40. }