Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

phpPatterns:
the Observer Pattern
Jan 10, 2003 @ 13:52:20

If you're looking for a pattern in PHP to help you out with a "one to many" relationship in a script you're working with, then this new pattern from phpPatterns.com might just be the ticket to help you out.

The Observer Pattern is designed to help cope with one to many relationships between objects, allowing changes in an object to update many associated objects. It provides a powerful mechanism to extend our applications, in terms of how they respond to events, without needing alter existing (and working) code. The basic principle behind the observer pattern is if you have some object, such as a Post object for a forum system, you can have other objects, such as a Mailer object, act as an observer and respond to any changes in the Post object, such as emailing relevant forums users that a new post has been added to the thread they were subscribed to.

Creating these "observers" inside the class is pretty simple, making something like a "trigger" for different happenings in the form. The example they give you is a mailer script that simulates sending a message to all the users subscribed to a specific thread in the forum.

tagged:

Link:


Trending Topics: