How to implement inversion of control in php

(*-*)浩
Release: 2023-02-24 17:20:01
Original
3061 people have browsed it

IoC, Inversion of Control, transfer of dependencies, dependence on abstraction rather than practice

DI, Dependency Injection , you don’t have to maintain the object’s dependencies in the code. The container automatically injects the dependencies into the specified object according to the configuration.

How to implement inversion of control in php

There are various stores in a certain area, and each store Stores sell four kinds of fruits: apples for ten yuan each, bananas for twenty yuan each, oranges for thirty yuan each, and watermelons for forty yuan each. Customers can purchase them at any store, and each store can provide total sales to the tax bureau at any time if needed. Forehead. (Recommended learning: PHP programming from entry to proficiency)

Initial code implementation

class Shop<br/>{<br/>    // 商店的名字<br/>    private $name;<br/><br/>    // 商店的总销售额<br/>    private $turnover = 0;<br/><br/>    public function __construct($name){<br/>        $this->name = $name;<br/>    }<br/><br/>    // 售卖商品<br/>    public function sell($commodity){<br/>        switch ($commodity){<br/>            case &#39;apple&#39;:<br/>                $this->turnover += 10;<br/>                echo "卖出一个苹果<br/>";<br/>                break;<br/>            case &#39;banana&#39;:<br/>                $this->turnover += 20;<br/>                echo "卖出一个香蕉<br/>";<br/>                break;<br/>            case &#39;orange&#39;:<br/>                $this->turnover += 30;<br/>                echo "卖出一个橘子<br/>";<br/>                break;<br/>            case &#39;watermelon&#39;:<br/>                $this->turnover += 40;<br/>                echo "卖出一个西瓜<br/>";<br/>                break;<br/>        }<br/>    }<br/>    // 显示商店目前的总销售额<br/>    public function getTurnover(){<br/>        echo $this->name.&#39;目前为止的销售额为:&#39;.$this->turnover;<br/>    }<br/>}<br/><br/>// 顾客类<br/>class Human<br/>{<br/>    //从商店购买商品<br/>    public function buy(Shop $shop,$commodity){<br/>        $shop->sell($commodity);<br/>    }<br/>}<br/><br/>// new一个名为kfc的商店<br/>$kfc = new Shop(&#39;kfc&#39;);<br/>// new一个名为mike的顾客<br/>$mike = new Human();<br/><br/>// mike从kfc买了一个苹果<br/>$mike->buy($kfc,&#39;apple&#39;);<br/>// mike从kfc买了一个香蕉<br/>$mike->buy($kfc,&#39;banana&#39;);<br/><br/>// 输出kfc的总营业额<br/>echo $kfc->getTurnover();<br/>
Copy after login

It can be seen that although the code is completed to meet the current needs implementation, but the shell() method at this time relies on specific practices and has absolute control. Once we need to add a new product to the store, such as mango, we have to modify the sell() method of the store class, which violates the OCP principle, which is open to extensions and closed to modifications.

At this point we can modify the code as follows

abstract class Fruit<br/>{<br/>    public $name;<br/>    public $price;<br/>}<br/>class Shop<br/>{<br/>    //商店的名字<br/>    private $name;<br/><br/>    //商店的总销售额<br/>    private $turnover = 0;<br/><br/>    public function __construct($name){<br/>        $this->name = $name;<br/>    }<br/><br/>    //售卖商品<br/>    public function sell(Fruit $commodity){<br/>        $this->turnover += $commodity->price;<br/>        echo &#39;卖出一个&#39;.$commodity->name.&#39;,收入&#39;.$commodity->price."元<br/>";<br/>    }<br/><br/>    //显示商店目前的总销售额<br/>    public function getTurnover(){<br/>        echo $this->name.&#39;目前为止的销售额为:&#39;.$this->turnover;<br/>    }<br/>}<br/><br/>//顾客类<br/>class Human<br/>{<br/>    //从商店购买商品<br/>    public function buy(Shop $shop,$commodity){<br/>        $shop->sell($commodity);<br/>    }<br/>}<br/><br/>class Apple extends Fruit<br/>{<br/>    public $name = &#39;apple&#39;;<br/>    public $price = 10;<br/>}<br/>class Bananae extends Fruit<br/>{<br/>    public $name = &#39;banana&#39;;<br/>    public $price = 20;<br/>}<br/>class Orange extends Fruit<br/>{<br/>    public $name = &#39;orange&#39;;<br/>    public $price = 30;<br/>}<br/>class Watermelon extends Fruit<br/>{<br/>    public $name = &#39;watermelon&#39;;<br/>    public $price = 40;<br/>}<br/><br/>//new一个名为kfc的商店<br/>$kfc = new Shop(&#39;kfc&#39;);<br/>//new一个名为mike的顾客<br/>$mike = new Human();<br/><br/>//mike从kfc买了一个苹果<br/>$mike->buy($kfc,new Apple());<br/>//mike从kfc买了一个香蕉<br/>$mike->buy($kfc,new Bananae());<br/><br/>//输出kfc的总营业额<br/>echo $kfc->getTurnover();<br/>
Copy after login

The above code adds an abstract class named Fruit, and all fruits are independently inherited from Fruit class. At this time, the sell() method no longer relies on the specific fruit name, but on the abstract Fruit class. The control right to determine how much to sell is no longer included in the method, but is passed in from outside the method. This is inversion of control, and the process of achieving inversion of control is dependency injection.

Through inversion of control, when an object is created, an external entity that controls all objects in the system passes the reference of the object it depends on to it. It can also be said that dependencies are injected into the object.

The above is the detailed content of How to implement inversion of control in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!