What Is Dependency Injection?
Dependency injection is a technique where an object receives the other objects it depends on from the outside, rather than creating them itself internally.
Published October 7, 2026
Dependency injection is a technique where an object receives the other objects it depends on from the outside, rather than creating them itself internally.
Why it exists
- A class that creates its own dependencies internally is tightly coupled to specific implementations, making it hard to swap or mock them, especially in tests
- Injecting dependencies (usually via a constructor) lets calling code control exactly what implementation is provided
How to apply it
- Pass dependencies in through a constructor or setter method, rather than instantiating them with "new" inside the class
- This makes unit testing far easier, since a test can inject a fake/mock version of a dependency instead of the real one
FAQ
Do I need a special "DI framework" to use dependency injection?
No — the core technique is just passing dependencies in rather than creating them internally. DI frameworks/containers (common in Laravel, Spring, Angular) automate wiring those dependencies together at scale, but aren't required to use the pattern itself.