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

Tech.MyBuilder.com:
Dependency Injection tutorial
May 06, 2015 @ 16:16:25

If you've heard the term "dependency injection" but are still a little fuzzy on the concept, you should check out this new post to the MyBuilder.com site introducing you to some of the basic concepts and using it in some PHP-based examples. They focus on the use of a dependency injection container in this tutorial (versus something like constructor or setter injection).

A lot has been written about dependency injection or DI (for short) in PHP, but it's a bit difficult to find a tutorial showing how a DI container actually works. The objective of this post is to show the internals and concepts of a DI container.

They start with a brief look at what a dependency injection container is and how it's different from a service locator or regular autoloading. They mention some of the benefits it provides and get into the simple example. They start with a class that creates a hard dependency in the constructor and show how to refactor that out to constructor injection. They then take the next step and replace the constructor injection with a DI container injection and how its internals are set up to provide new instances of requested objects (via a services definition).

tagged: dependency injection di container tutorial introduction

Link: http://tech.mybuilder.com/dependency-injection-tutorial/


Trending Topics: