classSingleton{privatestatic$_instance=null;privatefunction__construct(){}publicfunction__clone(){trigger_error("clone method is not allowed.",E_USER_ERROR);}publicstaticfunctiongetInstance(){if(self::$_instance===null){self::$_instance=newself();}returnself::$_instance;}}
classSingleton{private$_handle=null;privatefunction__construct(){$this->_handle=newStdClass();}publicfunction__clone(){trigger_error("clone method is not allowed.",E_USER_ERROR);}publicstaticfunctiongetInstance(){static$_instance=null;if($_instance===null){$_instance=newself();}return$_instance;}publicfunctiongetObj(){return$this->_handle;}}classUserModelextendsSingleton{publicstaticfunctiongetUser(){var_dump(self::getInstance()->getObj());//logic no care}}classGroupModelextendsSingleton{publicstaticfunctiongetGroup(){var_dump(self::getInstance()->getObj());//logic no care}}UserModel::getUser();GroupModel::getGroup();
classSingleton{private$_handle=null;privatestatic$_instance=null;privatefunction__construct(){$this->_handle=newStdClass();}publicfunction__clone(){trigger_error("clone method is not allowed.",E_USER_ERROR);}publicstaticfunctiongetInstance(){if(self::$_instance===null){self::$_instance=newself();}returnself::$_instance;}publicfunctiongetObj(){return$this->_handle;}}classUserModelextendsSingleton{publicstaticfunctiongetUser(){var_dump(self::getInstance()->getObj());//logic no care}}classGroupModelextendsSingleton{publicstaticfunctiongetGroup(){var_dump(self::getInstance()->getObj());//logic no care}}UserModel::getUser();GroupModel::getGroup();
phpclassSingleton{privatestatic$_instances=array();protectedfunction__construct(){}finalpublicfunction__clone(){trigger_error("clone method is not allowed.",E_USER_ERROR);}finalpublicstaticfunctiongetInstance(){$c=get_called_class();if(!isset(self::$_instances[$c])){self::$_instances[$c]=new$c;}returnself::$_instances[$c];}}classCacheExtendsSingleton{private$_handle=null;protectedfunction__construct(){$this->_connect();}privatefunction_connect(){$this->_handle=newStdClass();}publicfunctiongetHandle(){return$this->_handle;}}classDbExtendsSingleton{private$_handle=null;protectedfunction__construct(){$this->_connect();}privatefunction_connect(){$this->_handle=newStdClass();}publicfunctiongetHandle(){return$this->_handle;}}