“单例模式”

设计模式之单例模式


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
class Single
{
protected $rnd;
protected static $ins =null ;

//将自动加载 设置为 protected类型
//这样没法new 类名进行实例化
protected function __construct() {
$this->rnd =rand(0,1000);
}

//声明一个静态方法
public static function getins(){
//判断self::$ins 保存着实例化之后的对象
if (self::$ins == null) {
self::$ins = new self();
}

return self::$ins;
}


}
?>