PHP - What is Dependency Injection
What is Dependency Injection
Dependency Injection (DI) is a design pattern that helps you write programs that are flexible, maintainable, and easy to test.
It means that instead of a class creating the objects it needs by itself, those objects are given (injected) to it from the outside.
When a class receives its dependencies rather than creating them, it becomes independent of specific implementations and easier to reuse.
What Is a Dependency
A dependency is any other object or service that a class needs to perform its work.
For example, if a class that manages students needs to access the database, the database connection becomes its dependency.
If the class creates its own database connection internally, it becomes tightly coupled to that specific implementation.
If we provide the database connection from the outside, the class becomes loosely coupled, which means it can work with any type of database connection without changing its internal code.
Why Dependency Injection Is Important
-
Loose Coupling – Classes do not depend directly on specific implementations. This makes them easier to change or replace.
-
Reusability – The same class can be used with different configurations or systems.
-
Testability – During testing, you can inject mock or fake objects instead of real ones. This simplifies unit testing.
-
Maintainability – Code is easier to maintain because changes in one component do not affect others.
-
Clarity – It becomes clear what dependencies a class needs because they are listed in one place, often the constructor.
How Dependency Injection Works Conceptually
When a class needs another object to perform a task, it does not create that object inside itself.
Instead, the needed object is created elsewhere and then supplied to the class.
This act of supplying or passing the dependency is known as injection.
Types of Dependency Injection
-
Constructor Injection
The dependencies are passed through the class constructor at the time of creating the object. This is the most common and recommended type because it ensures all required dependencies are available from the beginning. -
Setter Injection
The dependencies are passed after the object is created using specific setter methods. This is useful when a dependency is optional or may change later. -
Interface Injection
The dependency provides an interface that the dependent class implements. This allows the dependency to call the method on the dependent class to pass itself in. This form is less common in PHP but exists conceptually.
Real-World Analogy
Think of a student and a pen. The student needs a pen to write an exam.
-
Without dependency injection: the student must make the pen themselves before every exam.
-
With dependency injection: the teacher hands the pen to the student.
In the second case, the student can use any pen provided and focus only on writing.
That is how dependency injection works — you give the required tool to the class instead of making the class create it.
Role of Dependency Injection in Frameworks
Modern PHP frameworks such as Laravel, Symfony, and CodeIgniter 4 use Dependency Injection Containers (DIC).
These containers automatically create and manage dependencies for your classes.
When you define what a class needs, the container supplies those dependencies automatically, reducing manual setup and configuration.
Advantages Summary
-
Promotes loose coupling between components.
-
Improves testability by allowing mock dependencies.
-
Enhances reusability and maintainability.
-
Makes the system modular and easier to extend.
-
Encourages clear, organized, and cleaner architecture.
Key Terms to Remember
| Term | Meaning |
|---|---|
| Dependency | An external object or service a class needs to work. |
| Injection | The process of supplying that dependency from outside. |
| Loose Coupling | Design where classes are independent from one another. |
| Dependency Container | A system that automatically manages and injects dependencies. |