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[{FNAMEL}">get_class($obs)] = $obs;
}

function detach($obs) {
    {FNAMEL}">unset($this->observers[{FNAMEL}">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.

Categories: English Articles | 0 Comments
Defined tags for this entry: , , , ,
Tweet This!

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

No comments

Add Comment

You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Monster ID, Identicon/Ycon, Pavatar author images supported.

Submitted comments will be subject to moderation before being displayed.