unset($this->observers[$id]); } } public function notify() { foreach ($this->observers as $observer) { $observer->update($this); } } public function setValue($value) { $this->value = $value; $this->notify(); } public function getValue() { return $this->value; } } class Observer implements SplObserver { public function update(SplSubject $subject) { echo '<p>Новое значение равно: ' . $subject->getValue() . '</p>'; } } $subject = new Subject(); $observer = new Observer(); $subject->attach($observer); $subject->setValue(5);
return $this->_value; } } /** * 观察者1 */ class Observer1 implements SplObserver { public function update(SplSubject $subject) { echo '<b>对象</b>:</b>' . get_class() . ' <b>内容:</b>' . $subject->getValue() . '<br />'; } } /** * 观察者2 */ class Observer2 implements SplObserver { public function update(SplSubject $subject) { echo '<b>对象:</b>' . get_class() . ' <b>内容:</b>' . $subject->getValue() . '<br />'; } } //实现 $subject = new Subject(); $object1 = new Observer1(); $object2 = new Observer2(); $subject->attach($object1); $subject->attach($object2); $subject->setValue('这是内容');
public function testSetValue() { $newValue = 'NewValue'; $this->claim->setValue($newValue); $this->assertSame($newValue, $this->claim->getValue()); }