理解依赖注入 for Zend framework 2

依赖注入(Dependency Injection),也成为控制反转(Inversion of Control),一种设计模式,其目的是解除类之间的依赖关系。

假设我们需要举办一个Party,Party需要主持人、厨师、灯光、音响、食品、酒水等等。那么Party对他们存在依赖关系。用程序语言表示如下:

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的依赖,我们可以这么做:

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方法,可以使用接口。

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所有依赖的厨师、灯光、音响、食品、酒水都加进去,会是怎样?

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更像是一个“大工厂模式”。

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", "PathToCooker.php");
//直接返回实例
$di->set("Wine", new PathToWine.php);
//数组
$di->set("Light", array("className" => "PathToLight"));
 
$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 文章的一份拷贝,同为原创文章。

原文地址:https://www.cnblogs.com/jinglehit/p/3859729.html