class singT {
static protected $ins = null; //静态属性 保存getIns new的实例
public $sing = 0;
final protected function __construct(){ //构造阻止new
$this->sing = mt_rand(1,10000);
}
final protected function __clone(){ //阻止克隆
}
//开放一个公共静态方法给外部获取实例
static public function getIns(){
if (self::$ins instanceof self) {
return self::$ins;
}
self::$ins = new self();
return self::$ins;
}
}
$a = singT::getIns('sing');
$b = singT::getIns('sing');
print_r($a);
print_r($b);
简单单例模式
最新推荐文章于 2021-07-23 15:57:30 发布