理解依赖注入 for Zend framework 2

依赖注入(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 文章的一份拷贝,同为原创文章。框架

相关文章
相关标签/搜索