News Feed
Sections




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

PHPMaster.com:
Dependency Injection with Pimple
January 29, 2013 @ 09:37:50

On PHPMaster.com there's a new tutorial showing you how to use Pimple (the dependency injection container from the Symfony folks) in your application to manage objects and resources.

In application development, we try to create independent modules so that we can reuse code in future projects. But, it's difficult to create completely independent modules which provide useful functionality; their dependencies can cause maintenance nightmares unless they are managed properly. This is where Dependency Injection comes in handy, as it gives us the ability to inject the dependencies our code needs to function properly without hard coding them into the modules.

They start with a look at the problem with working with "concerete dependencies", ones that are hard-coded into your classes making them not only hard to test but potentially difficult to maintain. They include an example of this (a "SocialFeeds" class and friends) and then one of two ways to fix the situation. They start with using constructor-based injection, injecting the Twitter service into the main feeds object. They also talk about another method - setter-based injection - where the objects are injected via specific methods on the object.

As a third alternative, though, they get to using Pimple to manage the objects, making it easier to inject just the one resource into your classes and extract the objects you need from there. There's also a bit of "advanced" usage of Pimple showing the use of the "share" and "extend" methods.

0 comments voice your opinion now!
dependency injection pimple symfony constructor setter tutorial


blog comments powered by Disqus

Similar Posts

Matthew Turland's Blog: Renaming a DOMNode in PHP

Brian Deshong's Blog: Who’s Online with PHP and Memcached

Lorna Mitchell's Blog: POSTing JSON Data With PHP cURL

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

JellyandCustard.com: Using DirectoryIterator to List Files in PHP


Community Events

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


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

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