News Feed
Sections




News Archive
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 @ 12: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.

0 comments voice your opinion now!
spl splobserver splsubject refactor symfony webservice tutorial


blog comments powered by Disqus

Similar Posts

PHPMaster.com: Setting Custom Error Messages for Zend_Form_Element

Symfony Blog: First Online Symfony Community Hack Day: July 5th!

Stefan Koopmanschap's Blog: Distributing your project with symfony embedded

PHPMaster.com: An Intro to Virtual Proxies, Part 1

IBM developerWorks: Introduction to MVC programming with Agavi (Parts 4 & 5)


Community Events





Don't see your event here?
Let us know!


community install language symfony podcast library laravel version release composer introduction security package tool series framework opinion interview voicesoftheelephpant update

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework