0
0
PHPprogramming~5 mins

Dependency injection concept in PHP

Choose your learning style9 modes available
Introduction

Dependency injection helps your code get what it needs without creating it inside. This makes your code easier to change and test.

When a class needs another class to work, like a car needs an engine.
When you want to change parts of your program without rewriting everything.
When you want to test parts of your program by giving fake parts instead of real ones.
When you want to keep your code clean and easy to understand.
Syntax
PHP
<?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();
?>
The Engine is given to Car from outside, not created inside Car.
This way, you can give Car different engines without changing Car code.
Examples
Logger is injected into User so User can log messages without creating Logger itself.
PHP
<?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();
Shop depends on a payment method but does not create it. You can inject any payment gateway that follows the interface.
PHP
<?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);
Sample Program

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
<?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();
OutputSuccess
Important Notes

Dependency injection makes your code easier to test because you can give fake parts.

It helps keep your code clean and flexible.

Summary

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.