The Observer Pattern and PHP4
Matt Zandstra wrote a great and highly recommended article about The Observer Pattern for the Zend Developer Zone. The tutorial is geared at intermediate and expert users, and of course users of PHP5. Now, for those of you who - like me - sometimes still have to work with PHP Version Four, the example won't work out of the box.
Since the observer pattern came in quite handy for my current project, I'd like to share the minor fixes I had to apply to the code. The methods attach() and detach() are the only two methods you'll have to change. Just edit them like so:
function attach($obs) {
$this->observers[get_class($obs)] = $obs;
}
function detach($obs) {
unset($this->observers[get_class($obs)]);
}
I don't know why Matt used delete() to detach an observer because in the manual it says under delete(): This is a dummy manual entry to satisfy those people who are looking for unlink() or unset() in the wrong place
. Whatever, I hope some of you will find my entry useful when hunting for this specific problem.
Kommentare
Ansicht der Kommentare: Linear | Verschachtelt