You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

55 lines
1.6 KiB

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 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. /**
  16. * @var null|static 实例对象
  17. */
  18. protected static $instance = null;
  19. /**
  20. * 获取示例
  21. * @param array $options 实例配置
  22. * @return static
  23. */
  24. public static function instance($options = [])
  25. {
  26. if (is_null(self::$instance)) self::$instance = new self($options);
  27. return self::$instance;
  28. }
  29. /**
  30. * 静态调用
  31. * @param string $method 调用方法
  32. * @param array $params 调用参数
  33. * @return mixed
  34. * @throws Exception
  35. */
  36. public static function __callStatic($method, array $params)
  37. {
  38. if (is_null(self::$instance)) self::$instance = new self();
  39. $call = substr($method, 1);
  40. if (0 !== strpos($method, '_') || !is_callable([self::$instance, $call])) {
  41. throw new Exception("method not exists:" . $method);
  42. }
  43. return call_user_func_array([self::$instance, $call], $params);
  44. }
  45. }