News Feed
Sections




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

Fabien Potencier's Blog:
What is Dependency Injection?
March 27, 2009 @ 11:16:10

Fabien Potencier has posted a look at dependency injections - what they are and how they can effect your code (usually for the good).

Dependency Injection is probably one of the most dead simple design pattern I know. And odds are you have probably already used Dependency Injection. But it is also one of the most difficult one to explain well. I think it is partly due to the nonsense examples used in most introductions to Dependency Injection. I have tried to come up with examples that fits the PHP world better. As PHP is a language mainly used for web development, let's take a simple Web example.

His example uses a session variable, setting it to a language preference and wrapping a class around it to handle the getting and setting. The dependency injection comes in when he adds a SessionStorage class into the mix, a tool that could change the place and method where that session information is held. He suggests that the best place to set these kinds of dependencies is usually the constructor but it can be done as a setter or property injection too. It just depends on the need for the script at the time.

0 comments voice your opinion now!
dependency injection setter property constructor designpettern example


blog comments powered by Disqus

Similar Posts

Fabien Potencier's Blog: Create your own framework... on top of the Symfony2 Components (part 12)

Developer Tutorials Blog: RSS feeds in PHP: 3 simple steps to PHP RSS generation

Edd Mann: Tuples in PHP

Ralph Schindler's Blog: PHP Constructor Best Practices And The Prototype Pattern

Fabien Potencier's Blog: PHP Iterators and Streams are awesome


Community Events

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


interview opinion podcast introduction framework version extension laravel conference php7 application symfony2 library api community voicesoftheelephpant release example performance series

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