News Feed
Jobs Feed
Sections




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

Charles Sprayberry's Blog:
DI and global state
January 31, 2012 @ 09:24:47

In response to some of the comments made on his previous post about why you should use dependency injection in your applications, Charles Sprayberry is back with some more concrete examples showing how it all works with some code to back it up.

To help better explain each of the three aspects of DI I discussed in the previous article I'll be going over each more thoroughly and with those code examples requested. I'll be going through each point one at a time as the explanations will likely be of some length compared to the original post.

He starts with the "villain" of the story - the Singleton design pattern, a difficult to test method that lulls you into thinking you're not in the global scope. He talks about the problem of using this approach and how the Factory design pattern can be used to create an alternative. He changes up the example to create a "DbTableFactory" class that can be used to create the objects needed - in this case a "UserTable" object with the connection injected into it at construct time.

0 comments voice your opinion now!
dependency injection di factory singleton global designpattern


blog comments powered by Disqus

Similar Posts

Greg Beaver's Blog: Be careful of PEAR 1.4.4 and older installs when uninstalling a package

Artur Ejsmont's Blog: How to structure a Zend Framework application and it's dependencies

PHPMaster.com: The MVC Pattern and PHP, Part 1

Wouter Lagerweij's Blog: Reading Up: Books Every Programmer Should Read

Ossigeno Blog: Factory for everything


Community Events











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


application composer facebook opinion package security symfony2 unittest hack code framework component support podcast language introduction hhvm release overview install

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