Extending singletons in PHP

前端 未结 6 583
春和景丽
春和景丽 2021-01-30 11:28

I\'m working in a web app framework, and part of it consists of a number of services, all implemented as singletons. They all extend a Service class, where the singleton behavio

6条回答
  •  暖寄归人
    2021-01-30 11:47

    This is fixed Johan's answer. PHP 5.3+

    abstract class Singleton
    {
        protected function __construct() {}
        final protected function __clone() {}
    
        final public static function getInstance()
        {
            static $instance = null;
    
            if (null === $instance)
            {
                $instance = new static();
            }
    
            return $instance;
        }
    }
    

提交回复
热议问题