【PHP设计模式】单例设计模式

class Single
{
    private static $_instance;
    private function __construct()
    {
        echo "初始化一次<br/>";
    }
    private function __clone()
    {
        // TODO: Implement __clone() method.
    }

    public static function getInstance(){
        if(!self::$_instance instanceof self){
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    public function test()
    {
        echo "test<br/>";
    }
}

Single::getInstance()->test();

 

posted @ 2021-09-02 10:30  廖亚平  阅读(19)  评论(0)    收藏  举报