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.
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.
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.