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

James Morris' Blog:
Removing Dependencies with the Observer Pattern (SplObserver, SplSubject)
May 18, 2011 @ 17:05:43

James Morris has a new post to his blog talking about a way he's found to remove dependencies from parts of your code by using the Observer pattern (specifically with the SplObserver and SplSubject components of PHP's SPL libraries).

Working on a symfony app, you usually have a mix of domain objects that are used by symfony actions, interspersed with symfony specific code such as logging and sfContext type stuff. A common bad practice I see is symfony specific code peppered inside of domain objects that could be used elsewhere (such as inside of a Zend app or a script from the cli) but now can’t as they’re coupled to the symfony code. One way you can remove an unwanted dependency is to use the Observer pattern – the dependency is pushed from inside the subject object to the client code that initialises the subject.

He sets up a scenario where a symfony action hits a web service and there's a dependency on two other types of objects (a service and transport object). He starts with the code for this method and gradually refactors out those two dependencies, removing an sfContext call from inside the service client and adds attach/detach/notify methods to implement the SplObserver on the class.

tagged: spl splobserver splsubject refactor symfony webservice tutorial

Link:

Eric Adams' Blog:
Implementing the Observer Pattern with SplObserver and SplSubject
Jun 30, 2010 @ 17:43:34

In a recent post from Eric Adams, he shows how to use the SplObserver and SplSubject components of the Standard PHP Library (SPL) to make an implementation of the observer pattern for more reusable code.

As PHP applications grow into complex object-oriented systems, developers look to create centralized components to execute repetitive tasks. These include logging, emailing, redirects, and more. The Observer pattern is a commonly used design pattern to hook such components into an application during runtime, thereby making them reusable. Since PHP 5.1, there are two interfaces built into the Standard PHP Library (SPL) that can be implemented to use the Observer pattern in your application. They are SplSubject, and SplObserver.

He shows how to use them in an example of a custom error handler class that extends the SplSubject with its attach, detach and notify methods as well as logger and mailer classes that will use it to handle their exceptions.

tagged: splobserver splsibject tutorial observer designpattern

Link:

Zend Developer Zone:
Implementing the Observer Pattern with SplObserver and SplSubject
Jun 10, 2010 @ 17:15:36

New on the Zend Developer Zone today there's a new tutorial showing you how to set up a script that uses the Observer design pattern with the help of two bits of functionality from the SPL - SplObserver and SplSubject.

As PHP applications grow into complex object-oriented systems, developers look to create centralized components to execute repetitive tasks. These include logging, emailing, redirects, and more. The Observer pattern is a commonly used design pattern to hook such components into an application during runtime, thereby making them reusable. Since PHP 5.1, there are two interfaces built into the Standard PHP Library (SPL) that can be implemented to use the Observer pattern in your application. They are SplSubject, and SplObserver.

They create an exception handler that implements the SplSubject interface (and its "attach", "detach" and "notify" methods) to let the observers of the instance know when an exception is thrown. Code examples are included of both the basic example and a "Mailer" and "Logger" class that can watch the exception handler.

tagged: splobserver splsubject spl tutorial observer designpattern

Link:


Trending Topics: