PHP Dependency Injection:

Providing dependencies of one object to another object called Dependency Injection.

Dependency Injection involves four roles:

  1. A service object(s) to be used.
  2. A client object(s) that is depending upon service.
  3. An interface that define how client can use service
  4. An injector, which is responsible for constructing services and injecting them into clients

Advantages:

  • It allows independent development through which developer can implement classes that use each other with an abstraction layer called interface.
  • It decreases coupling between classes and its dependency.
  • It promotes re-usability, testability, maintainability.

Disadvantages:

  • It forces complexity to move out of classes and into the linkage between the classes which is not desierable or not easily understandable
  • It encourage to depend on Dependency Injection Framework.
  • It is difficult to read or trace how flow works.

PHP Dependency Injection and Dependency Injection Content are two different things. So in this article I will only explain Dependency Injection with some example. So if you want to know PHP Dependency Injection then needs to know Dependency Inversion Principle.

Dependency Inversion Principle:

The control of dependencies reverted from one being called to the one calling.
HighLeven Class => Abstraction Layer => LowLevel Class.

Lets talk about this calling principle with class levels. Generally in software application, we see different level of classes:

  1. Low level class (Implements basics and primary operations).
  2. High level class (encapsulate all complex logic)

For an example one module called Pagecreation which will take character from keyboard and writes them into printer device.

  1. High level class: createPage class contains logic of creating page
  2. Low level class are readFromKeyBoard, writeToPrinter

In bad design , the high level classes heavily depends on low level classes. In such cases if we want to change or add some extra feature in writeToPrinter class after implementation then we have to change createPage class also. We can change if not complex one, lets think if it is a complex one then it will be really hard to change and all time we will rub our head.

For avoiding such problem we can implements an abstraction layer like interface in between high level and low level.

  1. Highlevel class should not depends upon low level class, bot the should depends upon abstraction
  2. Abstraction should not depends upon details . Details should depends upon abstraction.

HighLeven Class => Abstraction Layer => LowLevel Class.HighLeven Class => Abstraction Layer => LowLevel Class.

We can also differentiate classic php codes with dependency injection code.

Classic PHP code:

This is how code is not using DI
Application needs A(e.g: a controller)
Application creates A
Application calls A

A needs B(e.g: a service)
A creates B
A calls B

B needs C(e.g: a service, repository, …..)
B creates C
B calls C and does something

DI PHP code:

This is how code is using DI

Application needs A, which needs B, which needs C also.
Application creates C
Application creates B and give it to C
Application creates A and give it to B
Application calls A

A calls B
B does something

Understanding with example:

Example of comparing a classic implementation(using new or singletons) Vs using DI

classic implementation:

Lets say we have:

and

Now we want use OpenStreetMaps instead of GoogleMaps. I can simply change but what issue is , we have to change the code in StoreService and allother class which uses GoogleMaps. what simplifies here is without DI all codes are tightly coupled each other.

Using DI:

and the services that are defined using Interface.

Now it is for the user of StoreService which implementation need to use and it can be changed anytime without having rewrite to StoreService. Here classes are no longer tightly coupled eachother.

So we can say there are three types of Injection

  1. Constructor Injection
  2. Setter(Method) Injection:
  3. Interface Injection:

Constructor Injection:

This methods requires the client to provide a parameter inside Constructor for dependency

Setter(Method) Injection:

This methods requires the client to provide setter method for dependency

Interface Injection:

It is used as abstraction layer , how the injector should talk to client while injecting dependencies.

and the services that are defined using Interface.

 

 

PHP Dependency Injection
The following two tabs change content below.
Jyotiranjan is one of passionate programmer who has been working in magento since 6 years and in the mean time he has developed enormous amount of modules and customisation of sites. He loves to write blogs and travel for long journeying.

Latest posts by Jyotiranjan Biswal (see all)

Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *