php设计模式-单例模式

<?php
/*
单例模式:保证一个类仅有一个实例
*/
class Singleton
{
    static private $_instance = null;
    private function __construct()
    {
    
    }
    
    static public function getInstance()
    {
        if(is_null(self::$_instance))
        {
            self::$_instance = new Singleton();
        }
    }
    
    public function display()
    {
        echo "it is a singlton class function";
    }
}

//$obj = new Singleton();

//var_dump($obj);
//$obj->display();
$obj = Singleton::getInstance();

$obj1 = Singleton::getInstance();
var_dump($obj === $obj1);

 

posted @ 2013-09-05 11:09  北斗极星  阅读(174)  评论(0编辑  收藏  举报