2
ZE2 Late Static Binding and Singleton
5
abstract class Singleton
7
static private $instances = array();
8
static private $nextInstanceId = 0;
9
private $instanceId = NULL;
10
static final public function getInstance()
12
$caller = get_called_class();
13
if (!isset(self::$instances[$caller])) {
14
self::$instances[$caller] = new $caller;
15
self::$instances[$caller]->instanceId = self::$nextInstanceId++;
17
return self::$instances[$caller];
19
public final function getInstanceId()
21
return $this->instanceId;
23
public final function identify()
29
class Foo extends Singleton {
32
class Bar extends Singleton {
35
class Baz extends Bar {
38
$u = Foo::getInstance();
39
$v = Bar::getInstance();
40
$w = Baz::getInstance();
46
$x = Foo::getInstance();
47
$y = Bar::getInstance();
48
$z = Baz::getInstance();
60
["instanceId":"Singleton":private]=>
64
["instanceId":"Singleton":private]=>
68
["instanceId":"Singleton":private]=>
72
["instanceId":"Singleton":private]=>
76
["instanceId":"Singleton":private]=>
80
["instanceId":"Singleton":private]=>
84
["instanceId":"Singleton":private]=>
88
["instanceId":"Singleton":private]=>
92
["instanceId":"Singleton":private]=>