设计模式--单例模式

2021-05-02

应用中有且只有一个实例,多次实例化的对象时完全相同的(严格来说单例模式只能在内部实例化,但是也可以在外部实例化)

<?

class Danli
{
	private static $_instance;

	private function __construct()
	{
	}

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

	private function __clone()
	{
	}
}

$a = Danli::getInstance();
$b = Danli::getInstance();
var_dump($a == $b);

 

{/if}