-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSingleton.php
More file actions
66 lines (60 loc) · 1.19 KB
/
Singleton.php
File metadata and controls
66 lines (60 loc) · 1.19 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* singleton
* @package mxcommon
*/
/**
* 单实例类
*
* 如果想要其它类也为单实例的,则继承此类,然后通过getInstance方法获取实例
*
* 要求php5.3以上
*
* 示例:
*
* class Foobar extends Singleton {};
*
* $foo = Foobar::getInstance();
*
* 注意,在php中应慎用单实例模式
* @author chenming
* @package mxcommon_lib
*/
class Singleton {
/**
* instance
* @var object
*/
protected static $instance = array();
/**
* construct
*/
protected function __construct(){
//Thou shalt not construct that which is unconstructable!
}
/**
* clone
* @return [type] [description]
*/
protected function __clone(){
//Me not like clones! Me smash clones!
}
/**
* get instance
* @return object return instance
*/
public static function getInstance(){
$called_class_name = get_called_class();
if (!isset($_instance[$called_class_name])){
$_instance[$called_class_name] = new $called_class_name();
$_instance[$called_class_name]->init();
return $_instance[$called_class_name];
}
}
/**
* init
*/
public function init(){
}
}
?>