summaryrefslogtreecommitdiff
path: root/Zend/tests/lsb_018.phpt
blob: 1fa560470030d084c987dbb09eaa706354b5ee60 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
--TEST--
ZE2 Late Static Binding and Singleton
--FILE--
<?php
abstract class Singleton
{
	static private $instances = array();
	static private $nextInstanceId = 0;
	private $instanceId = NULL;
	static final public function getInstance()
	{
		$caller = get_called_class();
		if (!isset(self::$instances[$caller])) {
			self::$instances[$caller] = new $caller;
			self::$instances[$caller]->instanceId = self::$nextInstanceId++;
		}
		return self::$instances[$caller];
	}
	public final function getInstanceId()
	{
		return $this->instanceId;
	}
	public final function identify()
	{
		var_dump($this);
	}
}

class Foo extends Singleton {
}

class Bar extends Singleton {
}

class Baz extends Bar {
}

$u = Foo::getInstance();
$v = Bar::getInstance();
$w = Baz::getInstance();

$u->identify();
$v->identify();
$w->identify();

$x = Foo::getInstance();
$y = Bar::getInstance();
$z = Baz::getInstance();

$u->identify();
$v->identify();
$w->identify();
$x->identify();
$y->identify();
$z->identify();
?>
===DONE===
--EXPECTF--
object(Foo)#%d (1) {
  ["instanceId":"Singleton":private]=>
  int(0)
}
object(Bar)#%d (1) {
  ["instanceId":"Singleton":private]=>
  int(1)
}
object(Baz)#%d (1) {
  ["instanceId":"Singleton":private]=>
  int(2)
}
object(Foo)#%d (1) {
  ["instanceId":"Singleton":private]=>
  int(0)
}
object(Bar)#%d (1) {
  ["instanceId":"Singleton":private]=>
  int(1)
}
object(Baz)#%d (1) {
  ["instanceId":"Singleton":private]=>
  int(2)
}
object(Foo)#%d (1) {
  ["instanceId":"Singleton":private]=>
  int(0)
}
object(Bar)#%d (1) {
  ["instanceId":"Singleton":private]=>
  int(1)
}
object(Baz)#%d (1) {
  ["instanceId":"Singleton":private]=>
  int(2)
}
===DONE===
--UEXPECTF--
object(Foo)#%d (1) {
  [u"instanceId":u"Singleton":private]=>
  int(0)
}
object(Bar)#%d (1) {
  [u"instanceId":u"Singleton":private]=>
  int(1)
}
object(Baz)#%d (1) {
  [u"instanceId":u"Singleton":private]=>
  int(2)
}
object(Foo)#%d (1) {
  [u"instanceId":u"Singleton":private]=>
  int(0)
}
object(Bar)#%d (1) {
  [u"instanceId":u"Singleton":private]=>
  int(1)
}
object(Baz)#%d (1) {
  [u"instanceId":u"Singleton":private]=>
  int(2)
}
object(Foo)#%d (1) {
  [u"instanceId":u"Singleton":private]=>
  int(0)
}
object(Bar)#%d (1) {
  [u"instanceId":u"Singleton":private]=>
  int(1)
}
object(Baz)#%d (1) {
  [u"instanceId":u"Singleton":private]=>
  int(2)
}
===DONE===