设计模式初探之单例模式

<?php
/**
* @note 懒汉式单例模式
*/
class Singleton{
//类实例
public static $instance;
public $a;

public static function getInstance()
{
if(!(self::$instance instanceof self)){
self::$instance = new self();
}
return self::$instance;
}
/**
* @note 私有化克隆方法防止克隆
*/
private function __clone()
{

}

}

$first = Singleton::getInstance();
//$first = new Singleton();
$second = Singleton::getInstance();
//$second = new Singleton();

//$first->a = '222222222222222';
//$second->a = 111111111111111;

echo '<pre>';
var_dump($first);
var_dump($second);
posted @ 2021-12-03 13:56  顾念如非  阅读(19)  评论(0)    收藏  举报