The Dependency Injection pattern is a design pattern that focuses on providing dependencies to an object from external sources rather than having the object create or manage its dependencies internally. It promotes loose coupling between classes, enhances reusability, and improves testability. In PHP, dependency injection can be implemented in several ways, including constructor injection, setter injection, and interface injection.
Constructor Injection:
Constructor injection involves passing dependencies to a class through its constructor. The dependencies are typically provided as constructor parameters.
class DatabaseConnection {
private $host;
private $username;
private $password;
private $database;
public function __construct($host, $username, $password, $database) {
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->database = $database;
}
public function connect() {
// Logic to establish a database connection.
echo "Connecting to the database: {$this->host}, {$this->username}, {$this->password}, {$this->database}<br>";
}
}
class UserRepository {
private $dbConnection;
public function __construct(DatabaseConnection $dbConnection) {
$this->dbConnection = $dbConnection;
}
public function getUser($id) {
$this->dbConnection->connect();
// Logic to retrieve a user from the database.
echo "Fetching user with ID: {$id}<br>";
}
}
// Usage example:
$dbConnection = new DatabaseConnection('localhost', 'username', 'password', 'database');
$userRepository = new UserRepository($dbConnection);
$userRepository->getUser(1);
In this example, the DatabaseConnection
class represents a database connection and expects its dependencies ($host, $username, $password, and $database
) to be passed through its constructor. The UserRepository
class depends on the DatabaseConnection
class and receives the instance through its constructor. The getUser()
method of the UserRepository
class can now utilize the dbConnection
object to fetch a user from the database.
Setter Injection:
Setter injection involves providing dependencies to a class through setter methods. The dependencies are set using the corresponding setter methods of the class.
class Logger {
private $logFilePath;
public function setLogFilePath($logFilePath) {
$this->logFilePath = $logFilePath;
}
public function log($message) {
// Logic to log the message to the specified file.
echo "Logging message '{$message}' to file: {$this->logFilePath}<br>";
}
}
class ProductService {
private $logger;
public function setLogger(Logger $logger) {
$this->logger = $logger;
}
public function createProduct($name, $price) {
// Logic to create a product.
$this->logger->log("Created product: {$name} - {$price}");
}
}
// Usage example:
$logger = new Logger();
$logger->setLogFilePath('/path/to/logfile.log');
$productService = new ProductService();
$productService->setLogger($logger);
$productService->createProduct('Product 1', 10.99);
In this example, the Logger
class represents a logging mechanism and expects the log file path to be set using the setLogFilePath()
method. The ProductService
class depends on the Logger class and receives the logger instance through its setLogger()
method. The createProduct()
method of the ProductService
class can now utilize the logger object to log the creation of a product.
Interface Injection:
Interface injection involves implementing an interface that defines the dependency and injecting the dependency through a dedicated method. This approach allows for more flexibility by allowing different implementations of the interface to be injected.
interface Mailer {
public function send($recipient, $subject, $message);
}
class SmtpMailer implements Mailer {
public function send($recipient, $subject, $message) {
// Logic to send an email using SMTP.
echo "Sending email to {$recipient} with subject '{$subject}' using SMTP.<br>";
}
}
class NotificationService {
private $mailer;
public function setMailer(Mailer $mailer) {
$this->mailer = $mailer;
}
public function sendNotification($recipient, $message) {
$this->mailer->send($recipient, 'Notification', $message);
}
}
// Usage example:
$smtpMailer = new SmtpMailer();
$notificationService = new NotificationService();
$notificationService->setMailer($smtpMailer);
$notificationService->sendNotification(This email address is being protected from spambots. You need JavaScript enabled to view it.', 'Hello, this is a notification.');
In this example, the Mailer
interface defines the contract for the mailer dependency. The SmtpMailer
class implements the Mailer interface and provides the implementation for sending an email using SMTP
.
The NotificationService
class depends on the Mailer
interface and receives the mailer implementation through its setMailer()
method. The sendNotification()
method of the NotificationService
class can now utilize the mailer object to send a notification email.
The Dependency Injection pattern helps achieve loose coupling between classes, promotes reusability, and simplifies testing by allowing dependencies to be easily mocked or replaced. It allows for greater flexibility in managing dependencies and enables more modular and maintainable code.