Introduction
Dependency injection helps your code get what it needs without creating it inside. This makes your code easier to change and test.
Dependency injection helps your code get what it needs without creating it inside. This makes your code easier to change and test.
<?php class Car { private $engine; public function __construct(Engine $engine) { $this->engine = $engine; } public function start() { $this->engine->run(); } } class Engine { public function run() { echo "Engine is running"; } } $engine = new Engine(); $car = new Car($engine); $car->start(); ?>
Engine is given to Car from outside, not created inside Car.Car different engines without changing Car code.<?php class Logger { public function log($message) { echo $message; } } class User { private $logger; public function __construct(Logger $logger) { $this->logger = $logger; } public function create() { $this->logger->log("User created\n"); } } $logger = new Logger(); $user = new User($logger); $user->create();
<?php interface PaymentGateway { public function pay($amount); } class Paypal implements PaymentGateway { public function pay($amount) { echo "Paid $amount using Paypal\n"; } } class Shop { private $paymentGateway; public function __construct(PaymentGateway $paymentGateway) { $this->paymentGateway = $paymentGateway; } public function checkout($amount) { $this->paymentGateway->pay($amount); } } $paypal = new Paypal(); $shop = new Shop($paypal); $shop->checkout(100);
This example shows how the Car class gets an Engine from outside. The Car does not create the Engine itself. This is dependency injection.
<?php // Engine class class Engine { public function run() { echo "Engine is running\n"; } } // Car class depends on Engine class Car { private $engine; public function __construct(Engine $engine) { $this->engine = $engine; } public function start() { $this->engine->run(); } } // Create Engine outside $engine = new Engine(); // Inject Engine into Car $car = new Car($engine); // Start the car $car->start();
Dependency injection makes your code easier to test because you can give fake parts.
It helps keep your code clean and flexible.
Dependency injection means giving a class what it needs from outside.
This helps change parts easily and test them separately.
It keeps your code clean and simple to understand.
dependency injection in PHP?class Logger {
public function log($msg) { echo "Log: $msg"; }
}
class User {
private $logger;
public function __construct(Logger $logger) {
$this->logger = $logger;
}
public function create() {
$this->logger->log('User created');
}
}
$logger = new Logger();
$user = new User($logger);
$user->create();class Mailer {
public function send($msg) { echo "Sending: $msg"; }
}
class Notification {
private $mailer;
public function __construct() {
$this->mailer = $mailer;
}
public function notify() {
$this->mailer->send('Hello');
}
}
$notification = new Notification();
$notification->notify();ReportGenerator class that depends on a DataFetcher and a Logger. Which is the best way to apply dependency injection here?DataFetcher and Logger as constructor parameters and assign them to properties. correctly injects both dependencies. Create new DataFetcher and Logger objects inside ReportGenerator methods. creates dependencies inside, breaking injection. Use global variables for DataFetcher and Logger inside ReportGenerator. uses globals, which is bad practice. Make ReportGenerator extend DataFetcher and Logger classes. tries multiple inheritance which PHP does not support.Pass the test and this pattern moves unknown