File: lsb_018.phpt

package info (click to toggle)
php5 5.6.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 150,376 kB
  • sloc: ansic: 727,510; php: 21,966; sh: 12,356; cpp: 8,763; xml: 6,105; yacc: 1,551; exp: 1,514; makefile: 1,461; pascal: 1,048; awk: 538; perl: 315; sql: 22
file content (95 lines) | stat: -rw-r--r-- 1,671 bytes parent folder | download | duplicates (9)
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
--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===