admin 发表于 2020-11-22 15:24:13

thinkphp6 理解 依赖注入(DI) or 控制反转(IoC)


<p><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">科普:</span><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">首先依赖注入和控制反转说的是同一个东西,是一种设计模式,这种设计模式用来减少程序间的耦合,鄙人学习了一下,看TP官网还没有相关的文章,就写下这篇拙作介绍一下这种设计模式,希望能为TP社区贡献一些力量。</span><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">首先先别追究这个设计模式的定义,否则你一定会被说的云里雾里,笔者就是深受其害,百度了N多文章,都是从理论角度来描述,充斥着大量的生涩词汇,要么就是java代码描述的,也生涩。</span><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">不管怎么样,总算弄清楚一些了,下面就以php的角度来描述一下依赖注入这个概念。</span><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">先假设我们这里有一个类,类里面需要用到数据库连接,按照最最原始的办法,我们可能是这样写这个类的:</span></p><ol class="linenums" style="margin-left: 40px; color: rgb(43, 165, 216); list-style-image: initial;"><li class="L0" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;example&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">{</span></li><li class="L1" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class="L2" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">private</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;$_db</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></li><li class="L3" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);"></span></li><li class="L4" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">function</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;__construct</span><span class="pun" style="color: rgb(102, 102, 0);">(){</span></li><li class="L5" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include&nbsp;</span><span class="str" style="color: rgb(0, 136, 0);">"./Lib/Db.php"</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></li><li class="L6" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="pln" style="color: rgb(0, 0, 0);">_db&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="typ" style="color: rgb(102, 0, 102);">Db</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"localhost"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="str" style="color: rgb(0, 136, 0);">"root"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="str" style="color: rgb(0, 136, 0);">"123456"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="str" style="color: rgb(0, 136, 0);">"test"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></li><li class="L7" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">}</span></li><li class="L8" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);"></span></li><li class="L9" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">function</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;getList</span><span class="pun" style="color: rgb(102, 102, 0);">(){</span></li><li class="L0" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="pln" style="color: rgb(0, 0, 0);">_db</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="pln" style="color: rgb(0, 0, 0);">query</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"......"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="com" style="color: rgb(136, 0, 0);">//这里具体sql语句就省略不写了</span></li><li class="L1" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">}</span></li><li class="L2" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="pun" style="color: rgb(102, 102, 0);">}</span></li></ol><div class="think-copy" style="position: absolute; top: -1px; right: -1px; width: 80px; height: 27px; line-height: 27px; text-align: center; border: 1px solid rgb(204, 204, 204);"><span style="display: inline-block; width: 80px; height: 27px;">复制代码</span></div><div class="zclip" id="zclip-ZeroClipboardMovie_1" style="position: absolute; left: 605px; top: -1px; width: 82px; height: 29px; z-index: 99;"></div><p><code class="prettyprint linenums lang-php prettyprinted" style="position: relative; border: 1px solid rgb(209, 215, 220); padding: 2px; font-size: 14px; display: block; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace, 微软雅黑; margin-top: 10px; margin-bottom: 10px; overflow-wrap: break-word; color: rgb(50, 50, 50);"></code><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">过程:</span><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">在构造函数里先将数据库类文件include进来;</span><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">然后又通过new Db并传入数据库连接信息实例化db类;</span><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">之后getList方法就可以通过$this-&gt;_db来调用数据库类,实现数据库操作。</span><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">看上去我们实现了想要的功能,但是这是一个噩梦的开始,以后example1,example2,example3....越来越多的类需要用到db组件,如果都这么写的话,万一有一天数据库密码改了或者db类发生变化了,岂不是要回头修改所有类文件?</span><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">ok,为了解决这个问题,工厂模式出现了,我们创建了一个Factory方法,并通过Factory::getDb()方法来获得db组件的实例:</span></p><ol class="linenums" style="margin-left: 40px; color: rgb(43, 165, 216); list-style-image: initial;"><li class="L0" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="typ" style="color: rgb(102, 0, 102);">Factory</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">{</span></li><li class="L1" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">static</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">function</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;getDb</span><span class="pun" style="color: rgb(102, 102, 0);">(){</span></li><li class="L2" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include&nbsp;</span><span class="str" style="color: rgb(0, 136, 0);">"./Lib/Db.php"</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></li><li class="L3" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">return</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="typ" style="color: rgb(102, 0, 102);">Db</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"localhost"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="str" style="color: rgb(0, 136, 0);">"root"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="str" style="color: rgb(0, 136, 0);">"123456"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="str" style="color: rgb(0, 136, 0);">"test"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></li><li class="L4" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">}</span></li><li class="L5" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="pun" style="color: rgb(102, 102, 0);">}</span></li></ol><div class="think-copy" style="position: absolute; top: -1px; right: -1px; width: 80px; height: 27px; line-height: 27px; text-align: center; border: 1px solid rgb(204, 204, 204);"><span style="display: inline-block; width: 80px; height: 27px;">复制代码</span></div><div class="zclip" id="zclip-ZeroClipboardMovie_2" style="position: absolute; left: 605px; top: -1px; width: 82px; height: 29px; z-index: 99;"></div><p><code class="prettyprint linenums lang-php prettyprinted" style="position: relative; border: 1px solid rgb(209, 215, 220); padding: 2px; font-size: 14px; display: block; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace, 微软雅黑; margin-top: 10px; margin-bottom: 10px; overflow-wrap: break-word; color: rgb(50, 50, 50);"></code><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">sample类变成:</span></p><ol class="linenums" style="margin-left: 40px; color: rgb(43, 165, 216); list-style-image: initial;"><li class="L0" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;example&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">{</span></li><li class="L1" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class="L2" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">private</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;$_db</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></li><li class="L3" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);"></span></li><li class="L4" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">function</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;__construct</span><span class="pun" style="color: rgb(102, 102, 0);">(){</span></li><li class="L5" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="pln" style="color: rgb(0, 0, 0);">_db&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="typ" style="color: rgb(102, 0, 102);">Factory</span><span class="pun" style="color: rgb(102, 102, 0);">::</span><span class="pln" style="color: rgb(0, 0, 0);">getDb</span><span class="pun" style="color: rgb(102, 102, 0);">();</span></li><li class="L6" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">}</span></li><li class="L7" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);"></span></li><li class="L8" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">function</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;getList</span><span class="pun" style="color: rgb(102, 102, 0);">(){</span></li><li class="L9" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="pln" style="color: rgb(0, 0, 0);">_db</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="pln" style="color: rgb(0, 0, 0);">query</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"......"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="com" style="color: rgb(136, 0, 0);">//这里具体sql语句就省略不写了</span></li><li class="L0" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">}</span></li><li class="L1" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="pun" style="color: rgb(102, 102, 0);">}</span></li></ol><div class="think-copy" style="position: absolute; top: -1px; right: -1px; width: 80px; height: 27px; line-height: 27px; text-align: center; border: 1px solid rgb(204, 204, 204);"><span style="display: inline-block; width: 80px; height: 27px;">复制代码</span></div><div class="zclip" id="zclip-ZeroClipboardMovie_3" style="position: absolute; left: 605px; top: -1px; width: 82px; height: 29px; z-index: 99;"></div><p><code class="prettyprint linenums lang-php prettyprinted" style="position: relative; border: 1px solid rgb(209, 215, 220); padding: 2px; font-size: 14px; display: block; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace, 微软雅黑; margin-top: 10px; margin-bottom: 10px; overflow-wrap: break-word; color: rgb(50, 50, 50);"></code><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">这样就完美了吗?再次想想一下以后example1,example2,example3....所有的类,你都需要在构造函数里通过Factory::getDb();获的一个Db实例,实际上你由原来的直接与Db类的耦合变为了和Factory工厂类的耦合,工厂类只是帮你把数据库连接信息给包装起来了,虽然当数据库信息发生变化时只要修改Factory::getDb()方法就可以了,但是突然有一天工厂方法需要改名,或者getDb方法需要改名,你又怎么办?当然这种需求其实还是很操蛋的,但有时候确实存在这种情况,一种解决方式是:</span><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">我们不从example类内部实例化Db组件,我们依靠从外部的</span><b style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">注入</b><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">,什么意思呢?看下面的例子:</span></p><ol class="linenums" style="margin-left: 40px; color: rgb(43, 165, 216); list-style-image: initial;"><li class="L0" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;example&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">{</span></li><li class="L1" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">private</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;$_db</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></li><li class="L2" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">function</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;getList</span><span class="pun" style="color: rgb(102, 102, 0);">(){</span></li><li class="L3" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="pln" style="color: rgb(0, 0, 0);">_db</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="pln" style="color: rgb(0, 0, 0);">query</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"......"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="com" style="color: rgb(136, 0, 0);">//这里具体sql语句就省略不写了</span></li><li class="L4" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">}</span></li><li class="L5" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="com" style="color: rgb(136, 0, 0);">//从外部注入db连接</span></li><li class="L6" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">function</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;setDb</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">$connection</span><span class="pun" style="color: rgb(102, 102, 0);">){</span></li><li class="L7" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="pln" style="color: rgb(0, 0, 0);">_db&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;$connection</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></li><li class="L8" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">}</span></li><li class="L9" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="pun" style="color: rgb(102, 102, 0);">}</span></li><li class="L0" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);"></span></li><li class="L1" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="com" style="color: rgb(136, 0, 0);">//调用</span></li><li class="L2" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">$example&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;example</span><span class="pun" style="color: rgb(102, 102, 0);">();</span></li><li class="L3" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">$example</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="pln" style="color: rgb(0, 0, 0);">setDb</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Factory</span><span class="pun" style="color: rgb(102, 102, 0);">::</span><span class="pln" style="color: rgb(0, 0, 0);">getDb</span><span class="pun" style="color: rgb(102, 102, 0);">());</span><span class="com" style="color: rgb(136, 0, 0);">//注入db连接</span></li><li class="L4" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">$example</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="pln" style="color: rgb(0, 0, 0);">getList</span><span class="pun" style="color: rgb(102, 102, 0);">();</span></li></ol><div class="think-copy" style="position: absolute; top: -1px; right: -1px; width: 80px; height: 27px; line-height: 27px; text-align: center; border: 1px solid rgb(204, 204, 204);"><span style="display: inline-block; width: 80px; height: 27px;">复制代码</span></div><div class="zclip" id="zclip-ZeroClipboardMovie_4" style="position: absolute; left: 605px; top: -1px; width: 82px; height: 29px; z-index: 99;"></div><p><code class="prettyprint linenums lang-php prettyprinted" style="position: relative; border: 1px solid rgb(209, 215, 220); padding: 2px; font-size: 14px; display: block; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace, 微软雅黑; margin-top: 10px; margin-bottom: 10px; overflow-wrap: break-word; color: rgb(50, 50, 50);"></code><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">这样一来,example类完全与外部类解除耦合了,你可以看到Db类里面已经没有工厂方法或Db类的身影了。我们通过从外部调用example类的setDb方法,将连接实例直接注入进去。这样example完全不用关心db连接怎么生成的了。</span><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">这就叫</span><b style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">依赖注入</b><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">,实现不是在代码内部创建依赖关系,而是让其作为一个参数传递,这使得我们的程序更容易维护,降低程序代码的耦合度,实现一种松耦合。</span><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">这还没完,我们再假设example类里面除了db还要用到其他外部类,我们通过:</span></p><ol class="linenums" style="margin-left: 40px; color: rgb(43, 165, 216); list-style-image: initial;"><li class="L0" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">$example</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="pln" style="color: rgb(0, 0, 0);">setDb</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Factory</span><span class="pun" style="color: rgb(102, 102, 0);">::</span><span class="pln" style="color: rgb(0, 0, 0);">getDb</span><span class="pun" style="color: rgb(102, 102, 0);">());</span><span class="com" style="color: rgb(136, 0, 0);">//注入db连接</span></li><li class="L1" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">$example</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="pln" style="color: rgb(0, 0, 0);">setFile</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Factory</span><span class="pun" style="color: rgb(102, 102, 0);">::</span><span class="pln" style="color: rgb(0, 0, 0);">getFile</span><span class="pun" style="color: rgb(102, 102, 0);">());</span><span class="com" style="color: rgb(136, 0, 0);">//注入文件处理类</span></li><li class="L2" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">$example</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="pln" style="color: rgb(0, 0, 0);">setImage</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Factory</span><span class="pun" style="color: rgb(102, 102, 0);">::</span><span class="pln" style="color: rgb(0, 0, 0);">getImage</span><span class="pun" style="color: rgb(102, 102, 0);">());</span><span class="com" style="color: rgb(136, 0, 0);">//注入Image处理类</span></li><li class="L3" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="pun" style="color: rgb(102, 102, 0);">...</span></li></ol><div class="think-copy" style="position: absolute; top: -1px; right: -1px; width: 80px; height: 27px; line-height: 27px; text-align: center; border: 1px solid rgb(204, 204, 204);"><span style="display: inline-block; width: 80px; height: 27px;">复制代码</span></div><div class="zclip" id="zclip-ZeroClipboardMovie_5" style="position: absolute; left: 605px; top: -1px; width: 82px; height: 29px; z-index: 99;"></div><p><code class="prettyprint linenums lang-php prettyprinted" style="position: relative; border: 1px solid rgb(209, 215, 220); padding: 2px; font-size: 14px; display: block; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace, 微软雅黑; margin-top: 10px; margin-bottom: 10px; overflow-wrap: break-word; color: rgb(50, 50, 50);"></code><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">我们没完没了的写这么多set?累不累?</span><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">ok,为了不用每次写这么多行代码,我们又去弄了一个工厂方法:</span></p><ol class="linenums" style="margin-left: 40px; color: rgb(43, 165, 216); list-style-image: initial;"><li class="L0" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="typ" style="color: rgb(102, 0, 102);">Factory</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">{</span></li><li class="L1" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">public</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">static</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">function</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;getExample</span><span class="pun" style="color: rgb(102, 102, 0);">(){</span></li><li class="L2" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$example&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;example</span><span class="pun" style="color: rgb(102, 102, 0);">();</span></li><li class="L3" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$example</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="pln" style="color: rgb(0, 0, 0);">setDb</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Factory</span><span class="pun" style="color: rgb(102, 102, 0);">::</span><span class="pln" style="color: rgb(0, 0, 0);">getDb</span><span class="pun" style="color: rgb(102, 102, 0);">());</span><span class="com" style="color: rgb(136, 0, 0);">//注入db连接</span></li><li class="L4" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$example</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="pln" style="color: rgb(0, 0, 0);">setFile</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Factory</span><span class="pun" style="color: rgb(102, 102, 0);">::</span><span class="pln" style="color: rgb(0, 0, 0);">getFile</span><span class="pun" style="color: rgb(102, 102, 0);">());</span><span class="com" style="color: rgb(136, 0, 0);">//注入文件处理类</span></li><li class="L5" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$example</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="pln" style="color: rgb(0, 0, 0);">setImage</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Factory</span><span class="pun" style="color: rgb(102, 102, 0);">::</span><span class="pln" style="color: rgb(0, 0, 0);">getImage</span><span class="pun" style="color: rgb(102, 102, 0);">());</span><span class="com" style="color: rgb(136, 0, 0);">//注入Image处理类</span></li><li class="L6" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">return</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;$expample</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></li><li class="L7" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">}</span></li><li class="L8" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);"></span></li><li class="L9" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="pun" style="color: rgb(102, 102, 0);">}</span></li></ol><div class="think-copy" style="position: absolute; top: -1px; right: -1px; width: 80px; height: 27px; line-height: 27px; text-align: center; border: 1px solid rgb(204, 204, 204);"><span style="display: inline-block; width: 80px; height: 27px;">复制代码</span></div><div class="zclip" id="zclip-ZeroClipboardMovie_6" style="position: absolute; left: 605px; top: -1px; width: 82px; height: 29px; z-index: 99;"></div><p><code class="prettyprint linenums lang-php prettyprinted" style="position: relative; border: 1px solid rgb(209, 215, 220); padding: 2px; font-size: 14px; display: block; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace, 微软雅黑; margin-top: 10px; margin-bottom: 10px; overflow-wrap: break-word; color: rgb(50, 50, 50);"></code><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">实例化example时变为:</span></p><ol class="linenums" style="margin-left: 40px; color: rgb(43, 165, 216); list-style-image: initial;"><li class="L0" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">$example</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="typ" style="color: rgb(102, 0, 102);">Factory</span><span class="pun" style="color: rgb(102, 102, 0);">::</span><span class="pln" style="color: rgb(0, 0, 0);">getExample</span><span class="pun" style="color: rgb(102, 102, 0);">();</span></li><li class="L1" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">$example</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="pln" style="color: rgb(0, 0, 0);">getList</span><span class="pun" style="color: rgb(102, 102, 0);">();</span></li></ol><div class="think-copy" style="position: absolute; top: -1px; right: -1px; width: 80px; height: 27px; line-height: 27px; text-align: center; border: 1px solid rgb(204, 204, 204);"><span style="display: inline-block; width: 80px; height: 27px;">复制代码</span></div><div class="zclip" id="zclip-ZeroClipboardMovie_7" style="position: absolute; left: 605px; top: -1px; width: 82px; height: 29px; z-index: 99;"></div><p><code class="prettyprint linenums lang-php prettyprinted" style="position: relative; border: 1px solid rgb(209, 215, 220); padding: 2px; font-size: 14px; display: block; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace, 微软雅黑; margin-top: 10px; margin-bottom: 10px; overflow-wrap: break-word; color: rgb(50, 50, 50);"></code><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">似乎完美了,但是怎么感觉又回到了上面第一次用工厂方法时的场景?这确实不是一个好的解决方案,所以又提出了一个概念:</span><b style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">容器</b><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">,又叫做</span><b style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">IoC容器</b><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">、</span><b style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">DI容器</b><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">。</span><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">我们本来是通过setXXX方法注入各种类,代码很长,方法很多,虽然可以通过一个工厂方法包装,但是还不是那么爽,好吧,我们不用setXXX方法了,这样也就不用工厂方法二次包装了,那么我们还怎么实现依赖注入呢?</span><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">这里我们引入一个约定:在example类的构造函数里传入一个名为Di $di的参数,如下:</span></p><ol class="linenums" style="margin-left: 40px; color: rgb(43, 165, 216); list-style-image: initial;"><li class="L0" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="kwd" style="color: rgb(0, 0, 136);">class</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;example&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">{</span></li><li class="L1" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">private</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;$_di</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></li><li class="L2" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">function</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;__construct</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">Di</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">&amp;</span><span class="pln" style="color: rgb(0, 0, 0);">$di</span><span class="pun" style="color: rgb(102, 102, 0);">){</span></li><li class="L3" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="pln" style="color: rgb(0, 0, 0);">_di&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;$di</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></li><li class="L4" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">}</span></li><li class="L5" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="com" style="color: rgb(136, 0, 0);">//通过di容器获取db实例</span></li><li class="L6" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">function</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;getList</span><span class="pun" style="color: rgb(102, 102, 0);">(){</span></li><li class="L7" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="pln" style="color: rgb(0, 0, 0);">_di</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="kwd" style="color: rgb(0, 0, 136);">get</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">'db'</span><span class="pun" style="color: rgb(102, 102, 0);">)-&gt;</span><span class="pln" style="color: rgb(0, 0, 0);">query</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"......"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="com" style="color: rgb(136, 0, 0);">//这里具体sql语句就省略不写了</span></li><li class="L8" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">}</span></li><li class="L9" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="pun" style="color: rgb(102, 102, 0);">}</span></li><li class="L0" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);"></span></li><li class="L1" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">$di&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="typ" style="color: rgb(102, 0, 102);">Di</span><span class="pun" style="color: rgb(102, 102, 0);">();</span></li><li class="L2" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">$di</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="kwd" style="color: rgb(0, 0, 136);">set</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"db"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="kwd" style="color: rgb(0, 0, 136);">function</span><span class="pun" style="color: rgb(102, 102, 0);">(){</span></li><li class="L3" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">return</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="typ" style="color: rgb(102, 0, 102);">Db</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="str" style="color: rgb(0, 136, 0);">"localhost"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="str" style="color: rgb(0, 136, 0);">"root"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="str" style="color: rgb(0, 136, 0);">"root"</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="str" style="color: rgb(0, 136, 0);">"test"</span><span class="pun" style="color: rgb(102, 102, 0);">);</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;</span></li><li class="L4" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);"></span><span class="pun" style="color: rgb(102, 102, 0);">});</span></li><li class="L5" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">$example&nbsp;</span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="kwd" style="color: rgb(0, 0, 136);">new</span><span class="pln" style="color: rgb(0, 0, 0);">&nbsp;example</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">$di</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></li><li class="L6" style="padding-right: 5px; padding-left: 5px; border-left: 1px solid rgb(209, 215, 220); line-height: 23px; background-color: rgb(250, 250, 250);"><span class="pln" style="color: rgb(0, 0, 0);">$example</span><span class="pun" style="color: rgb(102, 102, 0);">-&gt;</span><span class="pln" style="color: rgb(0, 0, 0);">getList</span><span class="pun" style="color: rgb(102, 102, 0);">();</span></li></ol><div class="think-copy" style="position: absolute; top: -1px; right: -1px; width: 80px; height: 27px; line-height: 27px; text-align: center; border: 1px solid rgb(204, 204, 204);"><span style="display: inline-block; width: 80px; height: 27px;">复制代码</span></div><div class="zclip" id="zclip-ZeroClipboardMovie_8" style="position: absolute; left: 605px; top: -1px; width: 82px; height: 29px; z-index: 99;"></div><p><code class="prettyprint linenums lang-php prettyprinted" style="position: relative; border: 1px solid rgb(209, 215, 220); padding: 2px; font-size: 14px; display: block; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace, 微软雅黑; margin-top: 10px; margin-bottom: 10px; overflow-wrap: break-word; color: rgb(50, 50, 50);"></code><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">Di就是IoC容器,所谓容器就是存放我们可能会用到的各种类的实例,我们通过$di-&gt;set()设置一个名为db的实例,因为是通过回调函数的方式传入的,所以set的时候并不会立即实例化db类,而是当$di-&gt;get('db')的时候才会实例化,同样,在设计di类的时候还可以融入单例模式。</span><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">这样我们只要在全局范围内申明一个Di类,将所有需要注入的类放到容器里,然后将容器作为构造函数的参数传入到example,即可在example类里面从容器中获取实例。当然也不一定是构造函数,你也可以用一个 setDi(Di $di)的方法来传入Di容器,总之约定是你制定的,你自己清楚就行。</span><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">这样一来依赖注入以及关键的容器概念已经介绍完毕,剩下的就是在实际中使用并理解它吧!</span><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">本文如有错误,请务必指出,大家共同学习,共同进步。</span><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><br style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;"><span style="color: rgb(50, 50, 50); font-family: &quot;Century Gothic&quot;, &quot;Microsoft yahei&quot;;">转载</span><a href="http://www.thinkphp.cn/topic/12180.html">http://www.thinkphp.cn/topic/12180.html</a><br></p>
页: [1]
查看完整版本: thinkphp6 理解 依赖注入(DI) or 控制反转(IoC)