Dependency Injection (DI)
Dependency Injection (DI) is a design pattern where dependencies are provided from the outside instead of being created inside a class. It promotes loose coupling, flexibility, and testability in software development.
Dependency Injection (DI) is a design pattern where dependencies are provided from the outside instead of being created inside a class. It promotes loose coupling, flexibility, and testability in software development.
Dependency Injection (DI) is a design pattern in software development that deals with how objects acquire their dependencies. Instead of a class creating its own dependencies internally, they are “injected” from the outside, usually by a framework or container.
This approach promotes loose coupling, making software easier to test, maintain, and extend. DI is a common implementation of the broader principle known as Inversion of Control (IoC).
<?php
// Without DI
class UserService {
private $logger;
public function __construct() {
$this->logger = new FileLogger(); // tightly coupled
}
public function createUser($name) {
$this->logger->log("User created: " . $name);
}
}
<?php
// With DI
class UserService {
private $logger;
// Dependency is injected from outside
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
public function createUser($name) {
$this->logger->log("User created: " . $name);
}
}
In the second example, UserService no longer depends on a specific FileLogger. Any class implementing LoggerInterface can be injected, improving flexibility and testability.
Dependency Injection is supported by most modern frameworks:
Dependency Injection is a key design pattern that improves flexibility, testability, and maintainability in modern software systems. By externalizing object creation, it enables cleaner architecture and supports scalable, decoupled applic