PHP's Dependency Injection (Dependency Injection, DI for short)

1. What is dependence?

Suppose there is an order class Order and an inventory class Inventory. The order class needs the inventory class to check the stock status of the item and update the stock. In this case, the order class is dependent on the stock class. The order class will call the method of the inventory class to complete specific inventory check and update operations

<?php
// 定义库存类
class Inventory {
    public function checkStock($productId) {
        // 检查库存逻辑
        // ...
        return true;
    }

}
// 定义订单类
class Order {
    public function placeOrder($productId) {
        // 创建库存对象
         $inventory = new Inventory();
        if ($inventory->checkStock($productId)) {
            echo "可以下单";
        } else {
            echo "库存不足,无法下单!";
        }
    }
}


$order = new Order();
// 调用订单方法下单
$order->placeOrder(123456);
2. What is injection? 

Injection refers to the process of passing the dependencies of an object to the object through external means. Injection can be divided into three forms: Constructor Injection, Property Injection and Method Injection.

// 定义订单类
class Order {
    private $inventory;

    public function __construct(Inventory $inventory) {
        $this->inventory = $inventory;
    }

    public function placeOrder($productId) {
        if ($this->inventory->checkStock($productId)) {
            $this->inventory->updateStock($productId);
            echo "订单已成功下单!";
        } else {
            echo "库存不足,无法下单!";
        }
    }
}
// 创建库存对象
$inventory = new Inventory();
// 创建订单对象并注入库存对象
$order = new Order($inventory);
// 调用订单方法下单
$order->placeOrder(123456);
3. What is dependency injection?

 Dependency injection refers to moving the dependencies of an object from the object itself to another place, and the external container manages and injects these dependencies. That is, the object is no longer responsible for creating or finding dependencies, but receives dependencies through constructors, method parameters, or property injection. The advantage of this is that it makes objects more independent, reusable, and easy to test, because they only need to focus on their own responsibilities, and don't care about how to create or manage dependencies.

<?php
// 定义库存类
class Inventory {
    public function checkStock($productId) {
        // 检查库存逻辑
        // ...
        return true;
    }

}
// 定义订单类
class Order {
    private $inventory;

    public function __construct(Inventory $inventory) {
        $this->inventory = $inventory;
    }

    public function placeOrder($productId) {
        if ($this->inventory->checkStock($productId)) {
            $this->inventory->updateStock($productId);
            echo "订单已成功下单!";
        } else {
            echo "库存不足,无法下单!";
        }
    }
}
// 创建库存对象
$inventory = new Inventory();
// 创建订单对象并注入库存对象
$order = new Order($inventory);
// 调用订单方法下单
$order->placeOrder(123456);

Guess you like

Origin blog.csdn.net/weixin_39934453/article/details/131962022