PHP 依赖注入,从此不再考虑加载顺序
说这个话题之前先讲一个比较高端的思想--'依赖倒置原则'
"依赖倒置是一种软件设计思想,在传统软件中,上层代码依赖于下层代码,当下层代码有所改动时,上层代码也要相应进行改动,因此维护成本较高。而依赖倒置原则的思想是,上层不应该依赖下层,应依赖接口。意为上层代码定义接口,下层代码实现该接口,从而使得下层依赖于上层接口,降低耦合度,提高系统弹性"
上面的解释有点虚,下面我们以实际代码来解释这个理论
比如有这么条需求,用户注册完成后要发送一封邮件,然后你有如下代码:
先有邮件类'Email.class.php'
<span style="color: #0000ff;">class</span> <span style="color: #008080;">Mail</span><span style="color: #000000;">{ </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> send() { </span><span style="color: #008000;">/*</span><span style="color: #008000;">这里是如何发送邮件的代码</span><span style="color: #008000;">*/</span><span style="color: #000000;"> }}</span>
然后又注册的类'Register.class.php'
<span style="color: #0000ff;">class</span><span style="color: #000000;"> Register{ </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$_emailObj</span><span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> doRegister() { </span><span style="color: #008000;">/*</span><span style="color: #008000;">这里是如何注册</span><span style="color: #008000;">*/</span> <span style="color: #800080;">$this</span>->_emailObj = <span style="color: #0000ff;">new</span> <span style="color: #008080;">Mail</span><span style="color: #000000;">(); </span><span style="color: #800080;">$this</span>->_emailObj->send();<span style="color: #008000;">//</span><span style="color: #008000;">发送邮件</span><span style="color: #000000;"> }}</span>
然后开始注册
<span style="color: #0000ff;">include</span> 'Mail.class.php'<span style="color: #000000;">;</span><span style="color: #0000ff;">include</span> 'Register.class.php'<span style="color: #000000;">;</span><span style="color: #800080;">$reg</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Register();</span><span style="color: #800080;">$reg</span>->doRegister();
看起来事情很简单,你很快把这个功能上线了,看起来相安无事... xxx天过后,产品人员说发送邮件的不好,要使用发送短信的,然后你说这简单我把'Mail'类改下...
又过了几天,产品人员说发送短信费用太高,还是改用邮件的好... 此时心中一万个草泥马奔腾而过...
这种事情,常常在产品狗身上发生,无可奈何花落去...
以上场景的问题在于,你每次不得不对'Mail'类进行修改,代码复用性很低,高层过度依赖于底层。那么我们就考虑'依赖倒置原则',让底层继承高层制定的接口,高层依赖于接口。
<span style="color: #0000ff;">interface</span> <span style="color: #008080;">Mail</span><span style="color: #000000;">{ </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> send();}</span>
<span style="color: #0000ff;">class</span> Email <span style="color: #0000ff;">implements</span> <span style="color: #008080;">Mail</span><span style="color: #000000;">(){ </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> send() { </span><span style="color: #008000;">//</span><span style="color: #008000;">发送Email</span><span style="color: #000000;"> }}</span>
<span style="color: #0000ff;">class</span> SmsMail <span style="color: #0000ff;">implements</span> <span style="color: #008080;">Mail</span><span style="color: #000000;">(){ </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> send() { </span><span style="color: #008000;">//</span><span style="color: #008000;">发送短信</span><span style="color: #000000;"> }}</span>
<span style="color: #0000ff;">class</span><span style="color: #000000;"> Register{ </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$_mailObj</span><span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #008080;">Mail</span> <span style="color: #800080;">$mailObj</span><span style="color: #000000;">) { </span><span style="color: #800080;">$this</span>->_mailObj = <span style="color: #800080;">$mailObj</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> doRegister() { </span><span style="color: #008000;">/*</span><span style="color: #008000;">这里是如何注册</span><span style="color: #008000;">*/</span> <span style="color: #800080;">$this</span>->_mailObj->send();<span style="color: #008000;">//</span><span style="color: #008000;">发送信息</span><span style="color: #000000;"> }}</span>
下面开始发送信息
<span style="color: #008000;">/*</span><span style="color: #008000;"> 此处省略若干行 </span><span style="color: #008000;">*/</span><span style="color: #800080;">$reg</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Register();</span><span style="color: #800080;">$emailObj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Email();</span><span style="color: #800080;">$smsObj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> SmsMail();</span><span style="color: #800080;">$reg</span>->doRegister(<span style="color: #800080;">$emailObj</span>);<span style="color: #008000;">//</span><span style="color: #008000;">使用email发送</span><span style="color: #800080;">$reg</span>->doRegister(<span style="color: #800080;">$smsObj</span>);<span style="color: #008000;">//</span><span style="color: #008000;">使用短信发送</span><span style="color: #008000;">/*</span><span style="color: #008000;"> 你甚至可以发完邮件再发短信 </span><span style="color: #008000;">*/</span>
上面的代码解决了'Register'对信息发送类的依赖,使用构造函数注入的方法,使得它只依赖于发送短信的接口,只要实现其接口中的'send'方法,不管你怎么发送都可以。上例就使用了"注入"这个思想,就像注射器一样将一个类的实例注入到另一个类的实例中去,需要用什么就注入什么。当然"依赖倒置原则"也始终贯彻在里面。"注入"不仅可以通过构造函数注入,也可以通过属性注入,上面你可以可以通过一个"setter"来动态为"mailObj"这个属性赋值。
上面看了很多,但是有心的读者可能会发现标题中"从此不再考虑加载顺序"这个字眼,你上面的不还是要考虑加载顺序吗? 不还是先得引入信息发送类,然后在引入注册类,然后再实例化吗? 如果类一多,不照样晕!
确实如此,现实中有许多这样的案例,一开始类就那么多,慢慢的功能越来越多,人员越来越多,编写了很多类,要使用这个类必须先引入那个类,而且一定要确保顺序正确。有这么个例子, "a 依赖于b, b 依赖于c, c 依赖于 d, d 依赖于e", 要获取'a'的实例,你必须依次引入 'e,d,c,b'然后依次进行实例化,老的员工知道这个坑,跳过去了。某天来了个新人,他想实例化'a' 可是一直报错,他都不造咋回事,此时只能看看看'a'的业务逻辑,然后知道要先获取'b'的实例,然后在看'b'的业务逻辑,然后... 一天过去了,他还是没有获取到'a'的实例,然后领导来了...
那这个事情到底是新人的技术低下,还是当时架构人员的水平低下了?
现在切入话题,来实现如何不考虑加载顺序,在实现前就要明白要是不考虑加载顺序就意味着让程序自动进行加载自动进行实例化。类要实例化,只要保证完整的传递给'__construct'函数所必须的参数就OK了,在类中如果要引用其他类,也必须在构造函数中注入,否则调用时仍然会发生错误。那么我们需要一个类,来保存类实例化所需要的参数,依赖的其他类或者对象以及各个类实例化后的引用
该类命名为盒子 'Container.class.php', 其内容如下:
<span style="color: #008000;">/*</span><span style="color: #008000;">** 依赖注入类</span><span style="color: #008000;">*/</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Container{ </span><span style="color: #008000;">/*</span><span style="color: #008000;">* [email protected] array 存储各个类的定义 以类的名称为键 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$_definitions</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #008000;">/*</span><span style="color: #008000;">* [email protected] array 存储各个类实例化需要的参数 以类的名称为键 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$_params</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #008000;">/*</span><span style="color: #008000;">* [email protected] array 存储各个类实例化的引用 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$_reflections</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * @var array 各个类依赖的类 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$_dependencies</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 设置依赖 * @param string $class 类、方法 名称 * @param mixed $defination 类、方法的定义 * @param array $params 类、方法初始化需要的参数 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> set(<span style="color: #800080;">$class</span>, <span style="color: #800080;">$defination</span> = <span style="color: #0000ff;">array</span>(), <span style="color: #800080;">$params</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">()) { </span><span style="color: #800080;">$this</span>->_params[<span style="color: #800080;">$class</span>] = <span style="color: #800080;">$params</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->_definitions[<span style="color: #800080;">$class</span>] = <span style="color: #800080;">$this</span>->initDefinition(<span style="color: #800080;">$class</span>, <span style="color: #800080;">$defination</span><span style="color: #000000;">); } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 获取实例 * @param string $class 类、方法 名称 * @param array $params 实例化需要的参数 * @param array $properties 为实例配置的属性 * @return mixed </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> get(<span style="color: #800080;">$class</span>, <span style="color: #800080;">$params</span> = <span style="color: #0000ff;">array</span>(), <span style="color: #800080;">$properties</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">()) { </span><span style="color: #0000ff;">if</span>(!<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->_definitions[<span style="color: #800080;">$class</span><span style="color: #000000;">])) {</span><span style="color: #008000;">//</span><span style="color: #008000;">如果重来没有声明过 则直接创建</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->bulid(<span style="color: #800080;">$class</span>, <span style="color: #800080;">$params</span>, <span style="color: #800080;">$properties</span><span style="color: #000000;">); } </span><span style="color: #800080;">$defination</span> = <span style="color: #800080;">$this</span>->_definitions[<span style="color: #800080;">$class</span><span style="color: #000000;">]; </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">is_callable</span>(<span style="color: #800080;">$defination</span>, <span style="color: #0000ff;">true</span><span style="color: #000000;">)) {</span><span style="color: #008000;">//</span><span style="color: #008000;">如果声明是函数</span> <span style="color: #800080;">$params</span> = <span style="color: #800080;">$this</span>->parseDependencies(<span style="color: #800080;">$this</span>->mergeParams(<span style="color: #800080;">$class</span>, <span style="color: #800080;">$params</span><span style="color: #000000;">)); </span><span style="color: #800080;">$obj</span> = <span style="color: #008080;">call_user_func</span>(<span style="color: #800080;">$defination</span>, <span style="color: #800080;">$this</span>, <span style="color: #800080;">$params</span>, <span style="color: #800080;">$properties</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">elseif</span>(<span style="color: #008080;">is_array</span>(<span style="color: #800080;">$defination</span><span style="color: #000000;">)) { </span><span style="color: #800080;">$originalClass</span> = <span style="color: #800080;">$defination</span>['class'<span style="color: #000000;">]; </span><span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$definition</span>['class'<span style="color: #000000;">]); </span><span style="color: #008000;">//</span><span style="color: #008000;">difinition中除了'class'元素外 其他的都当做实例的属性处理</span> <span style="color: #800080;">$properties</span> = <span style="color: #008080;">array_merge</span>((<span style="color: #0000ff;">array</span>)<span style="color: #800080;">$definition</span>, <span style="color: #800080;">$properties</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">合并该类、函数声明时的参数</span> <span style="color: #800080;">$params</span> = <span style="color: #800080;">$this</span>->mergeParams(<span style="color: #800080;">$class</span>, <span style="color: #800080;">$params</span><span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$originalClass</span> === <span style="color: #800080;">$class</span><span style="color: #000000;">) {</span><span style="color: #008000;">//</span><span style="color: #008000;">如果声明中的class的名称和关键字的名称相同 则直接生成对象</span> <span style="color: #800080;">$obj</span> = <span style="color: #800080;">$this</span>->bulid(<span style="color: #800080;">$class</span>, <span style="color: #800080;">$params</span>, <span style="color: #800080;">$properties</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {</span><span style="color: #008000;">//</span><span style="color: #008000;">如果不同则有可能为别名 则从容器中获取</span> <span style="color: #800080;">$obj</span> = <span style="color: #800080;">$this</span>->get(<span style="color: #800080;">$originalClass</span>, <span style="color: #800080;">$params</span>, <span style="color: #800080;">$properties</span><span style="color: #000000;">); } } </span><span style="color: #0000ff;">elseif</span>(<span style="color: #008080;">is_object</span>(<span style="color: #800080;">$defination</span><span style="color: #000000;">)) {</span><span style="color: #008000;">//</span><span style="color: #008000;">如果是个对象 直接返回</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$defination</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">Exception</span>(<span style="color: #800080;">$class</span> . ' 声明错误!'<span style="color: #000000;">); } </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$obj</span><span style="color: #000000;">; } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 合并参数 * @param string $class 类、函数 名称 * @param array $params 参数 * @return array </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">function</span> mergeParams(<span style="color: #800080;">$class</span>, <span style="color: #800080;">$params</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">()) { </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$this</span>->_params[<span style="color: #800080;">$class</span><span style="color: #000000;">])) { </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$params</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$params</span><span style="color: #000000;">)) { </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">_params; } </span><span style="color: #800080;">$result</span> = <span style="color: #800080;">$this</span>->_params[<span style="color: #800080;">$class</span><span style="color: #000000;">]; </span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$params</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span> => <span style="color: #800080;">$value</span><span style="color: #000000;">) { </span><span style="color: #800080;">$result</span>[<span style="color: #800080;">$key</span>] = <span style="color: #800080;">$value</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span><span style="color: #000000;">; } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 初始化声明 * @param string $class 类、函数 名称 * @param array $defination 类、函数的定义 * @return mixed </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">function</span> initDefinition(<span style="color: #800080;">$class</span>, <span style="color: #800080;">$defination</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$defination</span><span style="color: #000000;">)) { </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">array</span>('class' => <span style="color: #800080;">$class</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">is_string</span>(<span style="color: #800080;">$defination</span><span style="color: #000000;">)) { </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">array</span>('class' => <span style="color: #800080;">$defination</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">is_callable</span>(<span style="color: #800080;">$defination</span>) || <span style="color: #008080;">is_object</span>(<span style="color: #800080;">$defination</span><span style="color: #000000;">)) { </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$defination</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">is_array</span>(<span style="color: #800080;">$defination</span><span style="color: #000000;">)) { </span><span style="color: #0000ff;">if</span>(!<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$defination</span>['class'<span style="color: #000000;">])) { </span><span style="color: #800080;">$definition</span>['class'] = <span style="color: #800080;">$class</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$defination</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">Exception</span>(<span style="color: #800080;">$class</span>. ' 声明错误'<span style="color: #000000;">); } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 创建类实例、函数 * @param string $class 类、函数 名称 * @param array $params 初始化时的参数 * @param array $properties 属性 * @return mixed </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">function</span> bulid(<span style="color: #800080;">$class</span>, <span style="color: #800080;">$params</span>, <span style="color: #800080;">$properties</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">list</span>(<span style="color: #800080;">$reflection</span>, <span style="color: #800080;">$dependencies</span>) = <span style="color: #800080;">$this</span>->getDependencies(<span style="color: #800080;">$class</span><span style="color: #000000;">); </span><span style="color: #0000ff;">foreach</span> ((<span style="color: #0000ff;">array</span>)<span style="color: #800080;">$params</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$index</span> => <span style="color: #800080;">$param</span><span style="color: #000000;">) {</span><span style="color: #008000;">//</span><span style="color: #008000;">依赖不仅有对象的依赖 还有普通参数的依赖</span> <span style="color: #800080;">$dependencies</span>[<span style="color: #800080;">$index</span>] = <span style="color: #800080;">$param</span><span style="color: #000000;">; } </span><span style="color: #800080;">$dependencies</span> = <span style="color: #800080;">$this</span>->parseDependencies(<span style="color: #800080;">$dependencies</span>, <span style="color: #800080;">$reflection</span><span style="color: #000000;">); </span><span style="color: #800080;">$obj</span> = <span style="color: #800080;">$reflection</span>->newInstanceArgs(<span style="color: #800080;">$dependencies</span><span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$properties</span><span style="color: #000000;">)) { </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$obj</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">foreach</span> ((<span style="color: #0000ff;">array</span>)<span style="color: #800080;">$properties</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$name</span> => <span style="color: #800080;">$value</span><span style="color: #000000;">) { </span><span style="color: #800080;">$obj</span>-><span style="color: #800080;">$name</span> = <span style="color: #800080;">$value</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$obj</span><span style="color: #000000;">; } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 获取依赖 * @param string $class 类、函数 名称 * @return array </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">function</span> getDependencies(<span style="color: #800080;">$class</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->_reflections[<span style="color: #800080;">$class</span><span style="color: #000000;">])) {</span><span style="color: #008000;">//</span><span style="color: #008000;">如果已经实例化过 直接从缓存中获取</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">array</span>(<span style="color: #800080;">$this</span>->_reflections[<span style="color: #800080;">$class</span>], <span style="color: #800080;">$this</span>->_dependencies[<span style="color: #800080;">$class</span><span style="color: #000000;">]); } </span><span style="color: #800080;">$dependencies</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #800080;">$ref</span> = <span style="color: #0000ff;">new</span> ReflectionClass(<span style="color: #800080;">$class</span>);<span style="color: #008000;">//</span><span style="color: #008000;">获取对象的实例</span> <span style="color: #800080;">$constructor</span> = <span style="color: #800080;">$ref</span>->getConstructor();<span style="color: #008000;">//</span><span style="color: #008000;">获取对象的构造方法</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$constructor</span> !== <span style="color: #0000ff;">null</span><span style="color: #000000;">) {</span><span style="color: #008000;">//</span><span style="color: #008000;">如果构造方法有参数</span> <span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$constructor</span>->getParameters() <span style="color: #0000ff;">as</span> <span style="color: #800080;">$param</span><span style="color: #000000;">) {</span><span style="color: #008000;">//</span><span style="color: #008000;">获取构造方法的参数</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$param</span>-><span style="color: #000000;">isDefaultValueAvailable()) {</span><span style="color: #008000;">//</span><span style="color: #008000;">如果是默认 直接取默认值</span> <span style="color: #800080;">$dependencies</span>[] = <span style="color: #800080;">$param</span>-><span style="color: #000000;">getDefaultValue(); } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {</span><span style="color: #008000;">//</span><span style="color: #008000;">将构造函数中的参数实例化</span> <span style="color: #800080;">$temp</span> = <span style="color: #800080;">$param</span>-><span style="color: #000000;">getClass(); </span><span style="color: #800080;">$temp</span> = (<span style="color: #800080;">$temp</span> === <span style="color: #0000ff;">null</span> ? <span style="color: #0000ff;">null</span> : <span style="color: #800080;">$temp</span>-><span style="color: #000000;">getName()); </span><span style="color: #800080;">$temp</span> = Instance::getInstance(<span style="color: #800080;">$temp</span>);<span style="color: #008000;">//</span><span style="color: #008000;">这里使用Instance 类标示需要实例化 并且存储类的名字</span> <span style="color: #800080;">$dependencies</span>[] = <span style="color: #800080;">$temp</span><span style="color: #000000;">; } } } </span><span style="color: #800080;">$this</span>->_reflections[<span style="color: #800080;">$class</span>] = <span style="color: #800080;">$ref</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->_dependencies[<span style="color: #800080;">$class</span>] = <span style="color: #800080;">$dependencies</span><span style="color: #000000;">; </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">array</span>(<span style="color: #800080;">$ref</span>, <span style="color: #800080;">$dependencies</span><span style="color: #000000;">); } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 解析依赖 * @param array $dependencies 依赖数组 * @param array $reflection 实例 * @return array $dependencies </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">function</span> parseDependencies(<span style="color: #800080;">$dependencies</span>, <span style="color: #800080;">$reflection</span> = <span style="color: #0000ff;">null</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">foreach</span> ((<span style="color: #0000ff;">array</span>)<span style="color: #800080;">$dependencies</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$index</span> => <span style="color: #800080;">$dependency</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$dependency</span><span style="color: #000000;"> instanceof Instance) { </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$dependency</span>->id !== <span style="color: #0000ff;">null</span><span style="color: #000000;">) { </span><span style="color: #800080;">$dependencies</span>[<span style="color: #800080;">$index</span>] = <span style="color: #800080;">$this</span>->get(<span style="color: #800080;">$dependency</span>-><span style="color: #000000;">id); } </span><span style="color: #0000ff;">elseif</span>(<span style="color: #800080;">$reflection</span> !== <span style="color: #0000ff;">null</span><span style="color: #000000;">) { </span><span style="color: #800080;">$parameters</span> = <span style="color: #800080;">$reflection</span>->getConstructor()-><span style="color: #000000;">getParameters(); </span><span style="color: #800080;">$name</span> = <span style="color: #800080;">$parameters</span>[<span style="color: #800080;">$index</span>]-><span style="color: #000000;">getName(); </span><span style="color: #800080;">$class</span> = <span style="color: #800080;">$reflection</span>-><span style="color: #000000;">getName(); </span><span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">Exception</span>('实例化类 ' . <span style="color: #800080;">$class</span> . ' 时缺少必要参数:' . <span style="color: #800080;">$name</span><span style="color: #000000;">); } } } </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$dependencies</span><span style="color: #000000;">; }}</span>
下面是'Instance'类的内容,该类主要用于记录类的名称,标示是否需要获取实例
<span style="color: #0000ff;">class</span><span style="color: #000000;"> Instance{ </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * @var 类唯一标示 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #800080;">$id</span><span style="color: #000000;">; </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 构造函数 * @param string $id 类唯一ID * @return void </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$id</span><span style="color: #000000;">) { </span><span style="color: #800080;">$this</span>->id = <span style="color: #800080;">$id</span><span style="color: #000000;">; } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 获取类的实例 * @param string $id 类唯一ID * @return Object Instance </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> getInstance(<span style="color: #800080;">$id</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> self(<span style="color: #800080;">$id</span><span style="color: #000000;">); }}</span>
然后我们在'Container.class.php'中还是实现了为类的实例动态添加属性的功能,若要动态添加属性,需使用魔术方法'__set'来实现,因此所有使用依赖加载的类需要实现该方法,那么我们先定义一个基础类 'Base.class.php',内容如下
<span style="color: #0000ff;">class</span><span style="color: #000000;"> Base{ </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 魔术方法 * @param string $name * @param string $value * @return void </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __set(<span style="color: #800080;">$name</span>, <span style="color: #800080;">$value</span><span style="color: #000000;">) { </span><span style="color: #800080;">$this</span>->{<span style="color: #800080;">$name</span>} = <span style="color: #800080;">$value</span><span style="color: #000000;">; }}</span>
然后我们来实现'A,B,C'类,A类的实例 依赖于 B类的实例,B类的实例依赖于C类的实例
'A.class.php'
<span style="color: #0000ff;">class</span> A <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Base{ </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$instanceB</span><span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __construct(B <span style="color: #800080;">$instanceB</span><span style="color: #000000;">) { </span><span style="color: #800080;">$this</span>->instanceB = <span style="color: #800080;">$instanceB</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> test() { </span><span style="color: #800080;">$this</span>->instanceB-><span style="color: #000000;">test(); }}</span>
'B.class.php'
<span style="color: #0000ff;">class</span> B <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Base{ </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$instanceC</span><span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __construct(C <span style="color: #800080;">$instanceC</span><span style="color: #000000;">) { </span><span style="color: #800080;">$this</span>->instanceC = <span style="color: #800080;">$instanceC</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> test() { </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->instanceC-><span style="color: #000000;">test(); }}</span>
'C.class.php'
<span style="color: #0000ff;">class</span> C <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Base{ </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> test() { </span><span style="color: #0000ff;">echo</span> 'this is C!'<span style="color: #000000;">; }}de</span>
然后我们在'index.php'中获取'A'的实例,要实现自动加载,需要使用SPL类库的'spl_autoload_register'方法,代码如下
<span style="color: #0000ff;">function</span> autoload(<span style="color: #800080;">$className</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">include_once</span> <span style="color: #800080;">$className</span> . '.class.php'<span style="color: #000000;">;}spl_autoload_register(</span>'autoload', <span style="color: #0000ff;">true</span>, <span style="color: #0000ff;">true</span><span style="color: #000000;">);</span><span style="color: #800080;">$container</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Container;</span><span style="color: #800080;">$a</span> = <span style="color: #800080;">$container</span>->get('A'<span style="color: #000000;">);</span><span style="color: #800080;">$a</span>->test();<span style="color: #008000;">//</span><span style="color: #008000;">输出 'this is C!'</span>
上面的例子看起来是不是很爽,根本都不需要考虑'B','C' (当然,这里B,C 除了要使用相应类的实例外,没有其他参数,如果有其他参数,必须显要调用'$container->set(xx)'方法进行注册,为其制定实例化必要的参数)。有细心同学可能会思考,比如我在先获取了'A'的实例,我在另外一个地方也要获取'A'的实例,但是这个地方'A'的实例需要其中某个属性不一样,我怎么做到?
你可以看到'Container' 类的 'get' 方法有其他两个参数,'$params' 和 '$properties' , 这个'$properties' 即可实现刚刚的需求,这都依赖'__set'魔术方法,当然这里你不仅可以注册类,也可以注册方法或者对象,只是注册方法时要使用回调函数,例如
<span style="color: #800080;">$container</span>->set('foo', <span style="color: #0000ff;">function</span>(<span style="color: #800080;">$container</span>, <span style="color: #800080;">$params</span>, <span style="color: #800080;">$config</span><span style="color: #000000;">){ </span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$params</span><span style="color: #000000;">); </span><span style="color: #008080;">print_r</span>(<span style="color: #800080;">$config</span><span style="color: #000000;">);});</span><span style="color: #800080;">$container</span>->get('foo', <span style="color: #0000ff;">array</span>('name' => 'foo'), <span style="color: #0000ff;">array</span>('key' => 'test'));
还可以注册一个对象的实例,例如
<span style="color: #0000ff;">class</span><span style="color: #000000;"> Test{ </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> mytest() { </span><span style="color: #0000ff;">echo</span> 'this is a test'<span style="color: #000000;">; }}</span><span style="color: #800080;">$container</span>->set('testObj', <span style="color: #0000ff;">new</span><span style="color: #000000;"> Test());</span><span style="color: #800080;">$test</span> = <span style="color: #800080;">$container</span>->get('testObj'<span style="color: #000000;">);</span><span style="color: #800080;">$test</span>->mytest();
以上自动加载,依赖控制的大体思想就是将类所要引用的实例通过构造函数注入到其内部,在获取类的实例的时候通过PHP内建的反射解析构造函数的参数对所需要的类进行加载,然后进行实例化,并进行缓存以便在下次获取时直接从内存取得
以上代码仅仅用于学习和实验,未经严格测试,请不要用于生产环境,以免产生未知bug
鄙人才疏学浅,有不足之处,欢迎补足!