function &getInstance() {

static $instance = array();

if (!$instance) { //instance数组为空,所以为!false

$instance[0] =& new Cache();

}

return $instance[0];

}

 

  private static $instance = null;
function getInstance2(){
if( get_class(self::$instance) != __CLASS__ ){
self::$instance = new Cache();
}
return self::$instance;
}

 

abstract class Singleton {

    protected static $__CLASS__ = __CLASS__;
    protected function __construct() {}
    
    abstract protected function init();
    
    public static function getInstance() {
        static $instance;
        
        $class = self::getClass();
        
        if ($instance === null) {
            $instance = new $class();
            $instance->init();
        }
        
        return $instance;
    protected function __construct() {
    }
    
    abstract protected function init();
    
    /**
     * Gets an instance of this singleton. If no instance exists, a new instance is created and returned.
     * If one does exist, then the existing instance is returned.
     */
    public static function getInstance() {
        static $instance;
        
        $class = self::getClass();
        
        if ($instance === null) {
            $instance = new $class();
            $instance->init();
        }
        
        return $instance;

    }

 

 

相关文章:

  • 2021-04-13
  • 2021-08-25
  • 2021-10-16
  • 2022-12-23
  • 2021-05-23
  • 2021-12-18
  • 2022-01-21
  • 2021-12-18
猜你喜欢
  • 2022-01-07
  • 2021-07-12
  • 2022-12-23
  • 2021-07-20
  • 2022-12-23
  • 2021-06-07
相关资源
相似解决方案