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);
原文地址:https://www.cnblogs.com/hubing/p/3302863.html