Facade.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2021 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. declare(strict_types=1);
  12. namespace think;
  13. class Facade
  14. {
  15. /**
  16. * 始终创建新的对象实例
  17. * @var bool
  18. */
  19. protected static $alwaysNewInstance;
  20. protected static $instance;
  21. /**
  22. * 获取当前Facade对应类名
  23. * @access protected
  24. * @return string
  25. */
  26. protected static function getFacadeClass()
  27. {}
  28. /**
  29. * 创建Facade实例
  30. * @static
  31. * @access protected
  32. * @param bool $newInstance 是否每次创建新的实例
  33. * @return object
  34. */
  35. protected static function createFacade(bool $newInstance = false)
  36. {
  37. $class = static::getFacadeClass() ?: 'think\DbManager';
  38. if (static::$alwaysNewInstance) {
  39. $newInstance = true;
  40. }
  41. if ($newInstance) {
  42. return new $class();
  43. }
  44. if (!self::$instance) {
  45. self::$instance = new $class();
  46. }
  47. return self::$instance;
  48. }
  49. // 调用实际类的方法
  50. public static function __callStatic($method, $params)
  51. {
  52. return call_user_func_array([static::createFacade(), $method], $params);
  53. }
  54. }