summaryrefslogtreecommitdiff
path: root/Zend/tests/lsb_018.phpt
blob: 6f05b5f53c78c312ee972677d5fe297482e4175a (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
--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();
?>
--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)
}