依賴注入(Dependency Injection),也成爲控制反轉(Inversion of Control),一種設計模式,其目的是解除類之間的依賴關係。php
假設咱們須要舉辦一個Party,Party須要主持人、廚師、燈光、音響、食品、酒水等等。那麼Party對他們存在依賴關係。用程序語言表示以下:html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//Party.php
class Party{ //主持人 private $_host; function __construct(){ include "./Host.php"; $this->_host = new Host(); } function startParty(){ $this->_host->sayHello(); } } //Host.php class Host{ private $_name; function sayHello(){ echo "My name is " . $this->_name; } } //main $party = new Party(); $party->startParty(); |
可見Party的運行依賴於Host,沒有Host,Party不能單獨運行,也不能單獨發佈爲組件。爲了解除Party對Host的依賴,咱們能夠這麼作:java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
//Party.php
class Party{ //主持人 private $_host; function __construct($host = ""){ if($host){ $this->_host = $host; } } function startParty(){ $this->_host->sayHello(); } function setHost($host){ $this->_host = $host; } } //Host.php class Host{ private $_name; function sayHello(){ echo "My name is " . $this->_name; } } //main $host = new Host(); $party = new Party(); $party->setHost($host);//或者 $party = new Party($host) $party->startParty(); |
此時Party類對Host類的依賴被移到外面,運行時Host類經過構造函數或者setter注入到Party中。Party自己能夠被單獨發 布。若是Host沒有sayHello方法,將其注入到Party中必然致使異常。爲了約束Host必須含有sayHello方法,可使用接口。
laravel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
//Party.php
class Party{ //主持人 private $_hostInterface; function __construct($host = ""){ if($host){ $this->_hostInterface = $host; } } function startParty(){ $this->_hostInterface->sayHello(); } function setHost($host){ $this->_hostInterface = $host; } } //HostInterface.php Interface HostInterface{ public function sayHello(); } //Host.php class Host implement HostInterface{ private $_name; function sayHello(){ echo "My name is " . $this->_name; } } //main $host = new Host(); $party = new Party(); $party->setHost($host);//或者 $party = new Party($host) $party->startParty(); |
這麼作實際上已經達到了解耦的目的,那麼下面我要把Party全部依賴的廚師、燈光、音響、食品、酒水都加進去,會是怎樣?web
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
//Party.php
class Party{ //主持人 private $_host; private $_cooker; private $_wine; private $_food; private $_music; private $_light; function __construct(){ } function startParty(){ $this->_host->sayHello(); } function setHost($host){ $this->_host = $host; } function setCooker($cooker){ $this->_cooker = $cooker; } function set Wine($wine){ $this->_wine = $wine; } //...等等 food, light, music } ] //Host.php class Host{ private $_name; function sayHello(){ echo "My name is " . $this->_name; } } //Cooker.php class Cooker{} class Wine{} clsas Light{} //...等等其餘類 //main $host = new Host(); $cooker = new Cooker(); $wine = new Wine(); //...等等 $party = new Party(); $party->setHost($host); $party->setCooker($cooker); $party->setWine($wine); $party->setFood($food); $part->setMusic($music); $part->setLight($light); $party->startParty(); |
代碼中大量的實例化和setter調用,是否能夠優化?咱們須要一個DI容器,在DI中管理各個類,由DI負責注入。此時的DI更像是一個「大工廠模式」。thinkphp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
//Party.php
class Party{ private $_di; function __construct(){ } function startParty(){ $this->_di->get("Host")->sayHello(); $this->_di->get("Cooker")->cook(); //... } function setDI($di){ $this->_di = $di; } } //HostInterface.php Interface HostInterface{ public function sayHello(); } //Host.php class Host implement HostInterface{ private $_name; function sayHello(){ echo "My name is " . $this->_name; } } //main $di = new DI(); //匿名函數形式 $di->set("Host", function(){ return new Host(); }); //類名 $di->set("Cooker", "Path\To\Cooker.php"); //直接返回實例 $di->set("Wine", new Path\To\Wine.php); //數組 $di->set("Light", array("className" => "Path\To\Light")); $party = new Party(); $party->setDI($di); $party->startParty(); |
DI使用set來註冊服務類,註冊的方式能夠有不少種,他們都是惰性實例化,set時並不實例化,在get時纔會實例化。而DI在註冊服務類時一般會使用配置來實現,如JSON、XML或者PHP數組。設計模式
DI很是注重約定,$di->get(「Host」)獲取的實例若是不是Host實例的話,將會引起異常。所以,DI屬於強約定模式,一般用 於底層架構,Zend framework 2的核心部分使用DI模式,但在框架應用層使用服務定位模式(ServiceLocator),服務定位模式與依賴注入很是類似,都可以解除類之間的依賴 關係,且實現思路與DI基本一致。數組
參考資料:
理解PHP 依賴注入
話說 依賴注入(DI) or 控制反轉(IoC)
用PHP實現簡單的控制反轉(IOC) 依賴注入(DI),用JSON配置文件架構
本文是做者的團隊博客ComingX上 理解依賴注入 for Zend framework 2 文章的一份拷貝,同爲原創文章。框架