6
class MyObjectStorage extends SplObjectStorage
10
echo __METHOD__ . "()\n";
16
echo __METHOD__ . "(" . (parent::valid() ? 1 : 0) . ")\n";
17
return parent::valid();
22
echo __METHOD__ . "(" . parent::key() . ")\n";
28
echo __METHOD__ . "(" . parent::current()->getName() . ")\n";
29
return parent::current();
34
echo __METHOD__ . "()\n";
39
class ObserverImpl implements SplObserver
43
function __construct($name = 'obj')
45
$this->name = '$' . $name;
48
function update(SplSubject $subject)
50
echo $this->name . '->' . __METHOD__ . '(' . $subject->getName() . ");\n";
59
class SubjectImpl implements SplSubject
64
function __construct($name = 'sub')
66
$this->observers = new MyObjectStorage;
67
$this->name = '$' . $name;
70
function attach(SplObserver $observer)
72
echo $this->name . '->' . __METHOD__ . '(' . $observer->getName() . ");\n";
73
$this->observers->attach($observer);
76
function detach(SplObserver $observer)
78
echo $this->name . '->' . __METHOD__ . '(' . $observer->getName() . ");\n";
79
$this->observers->detach($observer);
84
return $this->observers->count();
89
echo $this->name . '->' . __METHOD__ . "();\n";
90
foreach($this->observers as $key => $observer)
92
$observer->update($this);
101
function contains($obj)
103
return $this->observers->contains($obj);
107
$sub = new SubjectImpl;
109
$ob1 = new ObserverImpl("ob1");
110
$ob2 = new ObserverImpl("ob2");
111
$ob3 = new ObserverImpl("ob3");
113
var_dump($sub->contains($ob1));
115
var_dump($sub->contains($ob1));
119
var_dump($sub->count());
124
var_dump($sub->count());
130
var_dump($sub->count());
135
var_dump($sub->count());
144
$sub->SubjectImpl::attach($ob1);
146
$sub->SubjectImpl::attach($ob1);
147
$sub->SubjectImpl::attach($ob2);
148
$sub->SubjectImpl::attach($ob3);
150
$sub->SubjectImpl::notify();
151
MyObjectStorage::rewind()
152
MyObjectStorage::valid(1)
153
MyObjectStorage::current($ob1)
154
MyObjectStorage::key(0)
155
$ob1->ObserverImpl::update($sub);
156
MyObjectStorage::next()
157
MyObjectStorage::valid(1)
158
MyObjectStorage::current($ob2)
159
MyObjectStorage::key(1)
160
$ob2->ObserverImpl::update($sub);
161
MyObjectStorage::next()
162
MyObjectStorage::valid(1)
163
MyObjectStorage::current($ob3)
164
MyObjectStorage::key(2)
165
$ob3->ObserverImpl::update($sub);
166
MyObjectStorage::next()
167
MyObjectStorage::valid(0)
168
$sub->SubjectImpl::detach($ob3);
170
$sub->SubjectImpl::notify();
171
MyObjectStorage::rewind()
172
MyObjectStorage::valid(1)
173
MyObjectStorage::current($ob1)
174
MyObjectStorage::key(0)
175
$ob1->ObserverImpl::update($sub);
176
MyObjectStorage::next()
177
MyObjectStorage::valid(1)
178
MyObjectStorage::current($ob2)
179
MyObjectStorage::key(1)
180
$ob2->ObserverImpl::update($sub);
181
MyObjectStorage::next()
182
MyObjectStorage::valid(0)
183
$sub->SubjectImpl::detach($ob2);
184
$sub->SubjectImpl::detach($ob1);
186
$sub->SubjectImpl::notify();
187
MyObjectStorage::rewind()
188
MyObjectStorage::valid(0)
189
$sub->SubjectImpl::attach($ob3);
191
$sub->SubjectImpl::notify();
192
MyObjectStorage::rewind()
193
MyObjectStorage::valid(1)
194
MyObjectStorage::current($ob3)
195
MyObjectStorage::key(0)
196
$ob3->ObserverImpl::update($sub);
197
MyObjectStorage::next()
198
MyObjectStorage::valid(0)