What is Dependency Injection in PHP

What’s up, guys! Hers I am going to describe you the dependency Injection in PHP.

Dependency Injection in PHP:

Do you actually know what does it mean? Normally dependency injection means to inject something to another stuff. In programming concept, it means to inject an object to another class to perform an operation. In Software engineering dependency injection is really useful and handy design pattern. Programmers are widely using this design pattern to make their code more usable, testable and highly maintainable.

Programmers are always thinking how to manage their dependencies. They want to write good application code with avoiding dependencies. Here is the solution comes from design pattern concept to avoid the dependencies problem. I will use some code snippet to make you understand what is dependency Injection, why and how to use it.

Dependency Injection in PHP
Dependency Injection in PHP

Look at this code snippet:

At first glance of this code snippet, you might think there is nothing wrong with this code, For a moment I agree with you. But! if we have closer look on it then you can understand the problems of this code such as.

Here you can see the __construct of student class are creating an instance/object of course class. Which means we already written a hard-coded dependency for the student class. So the student and the course class are tightly coupled together. If we want to create instance/object of student class then we have to pass arguments for course class also. Cause student’s __construct is already creating instance/object of course class.

Now let’s imagine we made a change on course class. Suppose we add a new parameter in __construct method in course class. Then every class we need to change who created instance/object of course and they need to pass an extra parameter into course class.

 

Identifying The Problems:

Here you can see we added a new parameter into course class called lesson. And created a new class called department. So here student and department both classes needed course class and they are created instance/object for course class.

After adding a new parameter (lesson) in course class then student and department both classes are modified with passing a new parameter (lesson) to course class.

So every time you will need to do the same thing if the same case will happen. So if you have many classes like that, every class need to modify. And it’s a really annoying and error can occur easily. To avoid this types of scenario programmer are using dependency injection.

Solutions

Look at this code snippet, here you can see in student and department both class __construct needed course instance/object as a parameter (2nd parameter). And is assign into ‘$course’ properties.

We created an instance/object of course class, and while we creating instance/object for student and department then using created course instance/object as arguments.

So that way if we made any class in course class, then we don’t need to modify our student and department class. We just need to change in  $course = new Course('English', 2, 5);'
(where the object is created).

With this strategy, you can widely inject your dependency to another class. And your code will more testable and maintainable.

Hope this article hely you to understand what is dependency injection in PHP and how to use it.

Factory Method Design Pattern in PHP

Factory method design pattern is the most common design pattern in software engineering. It’s deals with creating object of a class without specified the class name. Just simply think there is a factory of creating object of other classes.

For example:

We can see there is two different classes, one is Student and another is StudentFactory.  StudentFactory is the factory of creating object of Student class. StudentFactory has a method called create() which used to return the object of Student class. So that we can easily create object of Student class from StudentFactory Class. These are the basic concept of Factory method design pattern in PHP.

Singleton Design Pattern

In software engineering, a singleton the design pattern which allowed only object of a class. That means singleton pattern restricts to create multi instance of a class. This is useful, when we need to create only one instance or object of a class. To do this we must need to ensure a mechanism where we can access that class without creating object for them.

This is very basic concept of  singleton design pattern.