-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleton.php
More file actions
49 lines (36 loc) · 771 Bytes
/
Copy pathSingleton.php
File metadata and controls
49 lines (36 loc) · 771 Bytes
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/**
* 单例模式
*/
class Singleton {
private static $instance;
private $name;
private function __construct()
{
// 构造方法私有化,外部不能直接实例化这个类
}
public static function getInstance()
{
if (self::$instance == null) {
self::$instance = new Singleton();
}
return self::$instance;
}
public function Get()
{
echo '发送Get请求...' . PHP_EOL;
}
public function Post()
{
echo '发送Post请求...' . PHP_EOL;
}
private function __clone()
{
// TODO: Implement __clone() method.
}
}
$a = Singleton::getInstance();
$b = Singleton::getInstance();
var_dump($a, $b);
$a->Get();
$b->Post();