态度决定高度、企图决定版图、格局决定结局

导航

php实现单件模式总结(持续更新)

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;

    }

 

 

posted on 2010-03-11 23:43  flyingchen  阅读(266)  评论(0编辑  收藏  举报