note 101224 deleted from class.splobserver by danbrown
| From: | danbrown@php.net | Date: | Fri, 03 Dec 2010 16:14:07 +0000 |
| Subject: | note 101224 deleted from class.splobserver by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-174589@lists.php.net to get a copy of this message | ||
Note Submitter: gmblar+php at gmail dot com
----
<?php
class observer implements SPLObserver {
public function update(SPLSubject $subject) {
echo get_class($subject), " has been updated. Test: ", $subject->getTest(),
"\n";
}
}
class subject implements SPLSubject {
protected $observers = array();
protected $test = 1;
public function attach(SPLObserver $observer) {
$id = spl_object_hash($observer);
$this->observers[$id] = $observer;
}
public function detach(SPLObserver $observer) {
$id = spl_object_hash($observer);
unset($this->observers[$id]);
}
public function notify() {
foreach($this->observers as $observer) {
$observer->update($this);
}
}
public function setTest($test) {
$this->test = $test;
$this->notify();
}
public function getTest() {
return $this->test;
}
}
$observer = new observer();
$subject = new subject();
$subject->attach($observer);
$subject->setTest(2);
$subject->detach($observer);
$subject->setTest(3);
?>