-
Notifications
You must be signed in to change notification settings - Fork 0
/
aSingleton.php
73 lines (68 loc) · 1.88 KB
/
aSingleton.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
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
67
68
69
70
71
72
73
<?php
namespace phpWTL;
/**
* Abstract class as a basis for Singleton.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
abstract class aSingleton {
/**
* Array to hold instances of all derived classes.
* @static
*/
protected static $_instance= array();
/**
* Create new instance or give back already existing one.
* @param array|mixed|null $inject Can be used to inject one or more parameter(s) into the constructor
* @return $_instance[$class] The instance of a derived class
* @static
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @api
*/
public static function getInstance($inject= null) {
$class= get_called_class();
if (false === array_key_exists($class, self::$_instance)) {
self::$_instance[$class]= new static($inject);
}
return self::$_instance[$class];
}
/**
* Disable cloning for singleton.
* @throws Exception if called
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
*/
protected function __clone() { throw new Exception("no cloning for singleton"); }
/**
* Disable serialization for singleton.
* @throws Exception if called
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
*/
protected function __sleep() { throw new Exception("no serialization for singleton"); }
/**
* Disable de-serialization for singleton.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.0
* @throws Exception if called
*/
protected function __wakeup() { throw new Exception("no de-serialization for singleton"); }
/**
* Constructor stump.
*
* @param object|null $inject Can be used to inject one or more parameter(s) into the constructor.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.1
* @api
*/
abstract protected function __construct($inject= null);
}
?>