Build your own Dependency Injection Container with PHP

Tram Ho

If you’ve worked with frameworks like Laravel, you probably know Service Container. The Service Container in Laravel is a Dependency Injection (DI) Container , là nơi quản lý class dependency và thực hiện dependency injection .

Using the Service Container is fairly simple, but understanding how it is built is another matter. In this article, I will introduce some knowledge that Laravel applies to building Service Container, so try to build 1 mini Dependency Injection (DI) Container similar to Service Container.

Content of the article :

  • Dependency Injection
  • Reflection (IMPORTANT)
  • Build mini DI Container

The most important of these is Reflection, which is a key issue for building DI containers.

Dependency Injection

If one Class A operation depends on several other Classes, instead of initializing instances of the other Classes inside Class A, we will inject those instances through the constructor or setter. Instances of the classes that Class A needs to operate are called dependencies.

For example:

It can be seen that the operation of the UserController requires an instance of the UserService , similarly for the operation of the UserService , it needs an instance of UserRepositoryInterface .

Here we have applied DI by injecting dependency classes via constructor as above example.

However when using the UserController

What if the class has many dependencies, and these dependencies have many other dependencies?

For example, UserService needs GroupService , GroupService needs GroupRepository :

This is when you need the DI Container to solve the problem. The strength of the DI Container is that you can “type-hint” the dependency in the constructor or method of the class, it will automatically resolve and inject it into the class for you. Using Reflection is how DI Containers do miracles.

Reflection

Verbatim from PHP :

PHP 5 comes with a complete reflection API that adds the ability to reverse-engineer classes, interfaces, functions, methods and extensions. Additionally, the reflection API offers ways to retrieve doc comments for functions, classes and methods.

Translating na: Reflection is the ability for the program to “inspect” itself, you can examine the class, interface, function, method and extension to see what its name, what it needs, … It can also get comments. of function, class, method.

I will introduce you a few build-in functions of Reflection that will apply to the construction of DI Container below, more detail you can go to the Reflection docs .

The Laravel Service Container is a DI Container, basically Laravel uses Reflection combined recursively to resolve dependencies from there to create the strength of the Service Container you have seen.

If you don’t believe it, go here https://github.com/laravel/framework/blob/master/src/Illuminate/Container/Container.php

At function build (line 793)

I don’t know how but from ReflectionClass , it must be true?

Build Mini DI Container

Ok now how to build DI like, please see the source code below, I have commented on each function. If you don’t understand anything, please comment below.

Mini DI Container

We will try to use the newly constructed Container class to apply to the first example.

Or you can bind 1 Closure (with params)

Above I have introduced you how to build 1 Dependency Injection Container. Hopefully from this article you can better understand how DI Container is built and how it works. If you have any questions or suggestions, comment below. Thank you 😊

Refer:

https://viblo.asia/p/laravel-beauty-tim-hieu-ve-service-container-3KbvZ1wLGmWB

https://viblo.asia/p/dependency-injection-hoat-dong-the-nao-rong-laravel-3Q75wD3JKWb

Share the news now

Source : Viblo