Dependency Injection (DI) Container in PHP

Mustafa Magdi
Dec 16, 2017 · 4 min read


We have introduced , clarifying what is Reflection API, and what is the different purposes of it, one of these -and most common- is using it with the DI Container, here is the article agenda:

  1. What is Dependency Injection.
  2. Different Ways of Injecting Objects (and Why?).
  3. What is DI Container.
  4. ReflectionClass & DI Container.
  5. Conclusion.
  6. References.

1. What is Dependency Injection

dependency injection is a technique whereby one object supplies the dependencies of another objectWhat is Dependency Injection — wikipedia

It is a very simple concept which you can inject an object to another object, check the following example:

class Profile {
public function deactivateProfile(Setting $setting)
$setting->isActive = false;

Because we need to use the $setting object inside the function, we inject/pass it as a parameter.

2. Different Ways of Injecting Objects (and Why?)

There are many ways to inject objects, here are a couple of most known:

  • Constructor Injection: it is how you inject an object through the class constructor:
class Profile {
private $setting;
public function __construct(Setting $setting)
$this->setting = $setting;
// to instantiate Profile, you have to instantiate Setting first
$setting = new Setting;
$profile = new Profile($setting);

Why?. This is the most common case, and it is useful to create a loosely coupled components. It is also useful while writing tests for your application.

  • Setter Injection: where you inject the object to your class through a setter function:
class Profile {
private $setting;
public function setSetting(Setting $setting)
$this->setting = $setting;
// to instantiate Profile, you have the option to inject Setting object
$setting = new Setting;
$profile = new Profile();

Why?. This way gives you the option to inject whenever you need to use it inside the Profile class.

3. What is DI Container

Dependency Injection Container is the way to manage injecting and reading objects and third party libraries in your application.

is telling you how to have a container in you application:

interface ContainerInterface {
public function get($id);
public function has($id);

It is a very simple implementation for the which has two main functions get() and has():
get(): get the object from the container.
has() : check if we have the object in the container.

Note: the aim of , is to put a standards for different implementations and introduce them to the PHP community, check more , and we will discuss it later in a different topic.

4. ReflectionClass & DI Container

Most of frameworks are using the DI Container not for just what you have seen in the preceding section, but with also resolving dependencies ().

But what do you mean by “resolving” dependencies?, it means the ability of your application to automatically handle the dependencies for a specific class instead of doing it manually.

Ok, let us make it more clear. Check the following example:

class Profile {
protected $setting;
public function __construct(Setting $setting)
$this->setting = $setting;

Here is the way we are instantiate Profile class:

$setting = new Setting;
$profile = new Profile($setting);

So, in order to instantiate Profile, you have to instantiate Setting first, and this how manually we are resolving dependencies for Profile class before instantiation.

We can do this in a neat way, here is a very simple example from for a Container class that is resolving dependencies for your application automatically:

The Container class is registering different classes via set() function:

$container = new Container();
// let our application know that we are going to use the following
// classes, and this is optional, because if you didn't do it, it
// will be registered via the get() function

And whenever you want to instantiate Profile class, you can easily do the following:

$profile = $container->get('Profile');

get() function is going through resolve() function, which is checking if the Profile class has any dependencies via its __construct() so it will resolve them recursively (means if Setting class has dependencies, they will be resolved as well), otherwise, it will instantiate Profile class direct.

ReflectionClass and ReflectionParameter are mainly used in our Container class:

$reflector = new ReflectionClass($concrete);
// check if class is instantiable
// get class constructor
// get new instance from class
// get new instance with dependencies resolved
// get constructor params
// get the type hinted class
// check if default value for a parameter is available
// get default value of parameter

5. Conclusion

We discussed one of Reflection API’s main usages and the main function of DI Container, which is and how we used the Reflection API effectively in it clarifying it with a simple implementation.

6. References

  1. Introduction about Reflection API:


tajawal tech blog

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store