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.

30 lines
783 B

7 years ago
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | PHP设计模式-单例模式
  4. // | 单例模式解决的是如何在整个项目中创建唯一对象实例的问题
  5. // +----------------------------------------------------------------------
  6. namespace app\common\lib;
  7. class Factory
  8. {
  9. private static $Factory;
  10. private function __construct()
  11. {
  12. }
  13. public static function getInstance($className, $options = null)
  14. {
  15. if(!isset(self::$Factory[$className]) || !self::$Factory[$className])
  16. {
  17. self::$Factory[$className] = new $className($options);
  18. }
  19. return self::$Factory[$className];
  20. }
  21. }
  22. /**
  23. * 示例
  24. * Factory::getInstance(\app\api\controller\Oauth::class);
  25. */