0%

PHP单例

PHP单例

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
26
27
28
29
30
31
<?php
/*
实现过程:
首先防止住外部随便的new操作
*/

class Single {
protected static $ins = null;
protected $hash;

private function __construct() {
$this->hash = rand(100000,999999);
}

public static function getInstance() {
if(self::$ins == null) {
self::$ins = new self();
}

return self::$ins;
}

private function __clone() {
}
}


$s1 = Single::getInstance();
$s2 = Single::getInstance();
?>

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!