News Feed
Sections




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

Court Ewing's Blog:
A Simple Alternative to Global Registry Dependency
December 09, 2010 @ 15:19:22

Court Ewing has written up a post looking at an alternative to a commonly used bad design pattern - the global registry dependency - a method that uses a singleton to control access to a class-wide instance of an object.

This gives you flexibility when configuring and setting your adapter, and it allows you to instantiate a new service without having to explicitly set commonly used dependencies, but you are ultimately just replacing one hardcoded object call with another. This means you are still limited in your ability to unit test the class properly, and you will have a difficult time debugging if you ever need to find out exactly when and where your database adapter was configured.

In his simple solution uses static methods to assign the database adapter when the bootstrap process is started instead of when the object is created. This adapter is assigned to an abstract class, so it's created even outside the class instantiations. There's one caveat to doing things this way, though - depending on the needs, you might have to have more than one abstract class and things could get tricky.

0 comments voice your opinion now!
alternative global registry dependency example


blog comments powered by Disqus

Similar Posts

Community News: Userland Naming Guide Launched

Zend Developer Zone: Lifting the Skirt on Zend Framework 1.5 - Zend_Form

Lorna Mitchell: Logging to Stdout with Monolog

PHPHacks.com: How to create an Ajax RSS reader with PHP

PHPMaster.com: Logging with PSR-3 to Improve Reusability


Community Events





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


language community laravel opinion symfony security series library composer list version podcast introduction tool voicesoftheelephpant framework conference interview artisanfiles release

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