News Feed
Sections




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

Allan MacGregor:
Playing with dependency injection in PHP
January 07, 2014 @ 10:08:10

Allan MacGregor has a recent post to his site looking at the use of Dependency Injection in PHP applications. He covers two main methods, constructor injection and setter injection, and code examples of each.

By using Dependency Injection we can write more maintainable, testable, and modular code. All projects have dependencies. The larger the project the more dependencies is it bound to have; now having a great number of dependencies is nothing bad by itself however how those dependencies are managed and maintained is.

Dependency Injection is not a new pattern and it has been commonly used on many languages like Java, but this pattern is somewhat new in the PHP world and it's gaining traction quickly thanks for frameworks like laravel.

He starts with a class that has no dependency injection, hard-coding dependencies inside the constructor. His first refactor shows how to take this class and use constructor injection to pass in an instance of an object rather than a value. After briefly explaining this method, he moves on to setter injection. He shows how to use a "setter" method to inject the needed object.

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

Link: http://coderoncode.com/2014/01/06/dependency-injection-php.html

blog comments powered by Disqus

Similar Posts

ITNewb.com: Introduction to JSON and PHP

C7Y: Step Away From the SuperGlobals! An Introduction to Inspekt

Nefarious Designs: Object-Oriented PHP Part 4: PHP5's Improved Features

Anson Cheung's Blog: Create nodes in eZ Publish using PHP

SitePoint PHP Blog: How to Create Your Own Twitter Widget in PHP, Part 1


Community Events

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


voicesoftheelephpant community video library framework introduction release podcast language laravel5 development conference unittest extension series psr7 interview opinion laravel api

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