懂得 PHP 依赖注入 | Laravel IoC容器

WBOY
Release: 2016-06-13 12:28:22
Original
812 people have browsed it

理解 PHP 依赖注入 | Laravel IoC容器

  Laravel框架的依赖注入确实很强大,并且通过容器实现依赖注入可以有选择性的加载需要的服务,减少初始化框架的开销,下面是我在网上看到的一个帖子,写的很好拿来与大家分享,文章从开始按照传统的类设计数据库连接一直到通过容器加载服务这个高度解耦的设计展示了依赖注入的强大之处,值得我们借鉴和学习。

-----------------------------------------------------------分割线下面是大牛的原文----------------------------------------------------------

      原文连接(http://www.yuansir-web.com/2014/03/20)

  首先,我们假设,我们要开发一个组件命名为SomeComponent。这个组件中现在将要注入一个数据库连接。在这个例子中,数据库连接在component中被创建,这种方法是不切实际的,这样做的话,我们将不能改变数据库连接参数及数据库类型等一些参数。

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> SomeComponent</span><span style="color: #008080;"> 4</span> <span style="color: #000000;">{</span><span style="color: #008080;"> 5</span> <span style="color: #008080;"> 6</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*</span><span style="color: #008080;"> 7</span> <span style="color: #008000;">     * The instantiation of the connection is hardcoded inside</span><span style="color: #008080;"> 8</span> <span style="color: #008000;">     * the component so is difficult to replace it externally</span><span style="color: #008080;"> 9</span> <span style="color: #008000;">     * or change its behavior</span><span style="color: #008080;">10</span>      <span style="color: #008000;">*/</span><span style="color: #008080;">11</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> someDbTask()</span><span style="color: #008080;">12</span> <span style="color: #000000;">    {</span><span style="color: #008080;">13</span>         <span style="color: #800080;">$connection</span> = <span style="color: #0000ff;">new</span> Connection(<span style="color: #0000ff;">array</span><span style="color: #000000;">(</span><span style="color: #008080;">14</span>             "host" => "localhost",<span style="color: #008080;">15</span>             "username" => "root",<span style="color: #008080;">16</span>             "password" => "secret",<span style="color: #008080;">17</span>             "dbname" => "invo"<span style="color: #008080;">18</span> <span style="color: #000000;">        ));</span><span style="color: #008080;">19</span> <span style="color: #008080;">20</span>         <span style="color: #008000;">//</span><span style="color: #008000;"> ...</span><span style="color: #008080;">21</span> <span style="color: #000000;">    }</span><span style="color: #008080;">22</span> <span style="color: #008080;">23</span> <span style="color: #000000;">}</span><span style="color: #008080;">24</span> <span style="color: #008080;">25</span> <span style="color: #800080;">$some</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> SomeComponent();</span><span style="color: #008080;">26</span> <span style="color: #800080;">$some</span>->someDbTask();
Copy after login

为了解决上面所说的问题,我们需要在使用前创建一个外部连接,并注入到容器中。就目前而言,这看起来是一个很好的解决方案:

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> SomeComponent</span><span style="color: #008080;"> 4</span> <span style="color: #000000;">{</span><span style="color: #008080;"> 5</span> <span style="color: #008080;"> 6</span>     <span style="color: #0000ff;">protected</span> <span style="color: #800080;">$_connection</span><span style="color: #000000;">;</span><span style="color: #008080;"> 7</span> <span style="color: #008080;"> 8</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*</span><span style="color: #008080;"> 9</span> <span style="color: #008000;">     * Sets the connection externally</span><span style="color: #008080;">10</span>      <span style="color: #008000;">*/</span><span style="color: #008080;">11</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setConnection(<span style="color: #800080;">$connection</span><span style="color: #000000;">)</span><span style="color: #008080;">12</span> <span style="color: #000000;">    {</span><span style="color: #008080;">13</span>         <span style="color: #800080;">$this</span>->_connection = <span style="color: #800080;">$connection</span><span style="color: #000000;">;</span><span style="color: #008080;">14</span> <span style="color: #000000;">    }</span><span style="color: #008080;">15</span> <span style="color: #008080;">16</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> someDbTask()</span><span style="color: #008080;">17</span> <span style="color: #000000;">    {</span><span style="color: #008080;">18</span>         <span style="color: #800080;">$connection</span> = <span style="color: #800080;">$this</span>-><span style="color: #000000;">_connection;</span><span style="color: #008080;">19</span> <span style="color: #008080;">20</span>         <span style="color: #008000;">//</span><span style="color: #008000;"> ...</span><span style="color: #008080;">21</span> <span style="color: #000000;">    }</span><span style="color: #008080;">22</span> <span style="color: #008080;">23</span> <span style="color: #000000;">}</span><span style="color: #008080;">24</span> <span style="color: #008080;">25</span> <span style="color: #800080;">$some</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> SomeComponent();</span><span style="color: #008080;">26</span> <span style="color: #008080;">27</span> <span style="color: #008000;">//</span><span style="color: #008000;">Create the connection</span><span style="color: #008080;">28</span> <span style="color: #800080;">$connection</span> = <span style="color: #0000ff;">new</span> Connection(<span style="color: #0000ff;">array</span><span style="color: #000000;">(</span><span style="color: #008080;">29</span>     "host" => "localhost",<span style="color: #008080;">30</span>     "username" => "root",<span style="color: #008080;">31</span>     "password" => "secret",<span style="color: #008080;">32</span>     "dbname" => "invo"<span style="color: #008080;">33</span> <span style="color: #000000;">));</span><span style="color: #008080;">34</span> <span style="color: #008080;">35</span> <span style="color: #008000;">//</span><span style="color: #008000;">Inject the connection in the component</span><span style="color: #008080;">36</span> <span style="color: #800080;">$some</span>->setConnection(<span style="color: #800080;">$connection</span><span style="color: #000000;">);</span><span style="color: #008080;">37</span> <span style="color: #008080;">38</span> <span style="color: #800080;">$some</span>->someDbTask();
Copy after login

  现在我们来考虑一个问题,我们在应用程序中的不同地方使用此组件,将多次创建数据库连接。使用一种类似全局注册表的方式,从这获得一个数据库连接实例,而不是使用一次就创建一次。

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Registry</span><span style="color: #008080;"> 4</span> <span style="color: #000000;">{</span><span style="color: #008080;"> 5</span> <span style="color: #008080;"> 6</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*</span><span style="color: #008080;"> 7</span> <span style="color: #008000;">     * Returns the connection</span><span style="color: #008080;"> 8</span>      <span style="color: #008000;">*/</span><span style="color: #008080;"> 9</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getConnection()</span><span style="color: #008080;">10</span> <span style="color: #000000;">    {</span><span style="color: #008080;">11</span>        <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> Connection(<span style="color: #0000ff;">array</span><span style="color: #000000;">(</span><span style="color: #008080;">12</span>             "host" => "localhost",<span style="color: #008080;">13</span>             "username" => "root",<span style="color: #008080;">14</span>             "password" => "secret",<span style="color: #008080;">15</span>             "dbname" => "invo"<span style="color: #008080;">16</span> <span style="color: #000000;">        ));</span><span style="color: #008080;">17</span> <span style="color: #000000;">    }</span><span style="color: #008080;">18</span> <span style="color: #008080;">19</span> <span style="color: #000000;">}</span><span style="color: #008080;">20</span> <span style="color: #008080;">21</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> SomeComponent</span><span style="color: #008080;">22</span> <span style="color: #000000;">{</span><span style="color: #008080;">23</span> <span style="color: #008080;">24</span>     <span style="color: #0000ff;">protected</span> <span style="color: #800080;">$_connection</span><span style="color: #000000;">;</span><span style="color: #008080;">25</span> <span style="color: #008080;">26</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*</span><span style="color: #008080;">27</span> <span style="color: #008000;">     * Sets the connection externally</span><span style="color: #008080;">28</span>      <span style="color: #008000;">*/</span><span style="color: #008080;">29</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setConnection(<span style="color: #800080;">$connection</span><span style="color: #000000;">){</span><span style="color: #008080;">30</span>         <span style="color: #800080;">$this</span>->_connection = <span style="color: #800080;">$connection</span><span style="color: #000000;">;</span><span style="color: #008080;">31</span> <span style="color: #000000;">    }</span><span style="color: #008080;">32</span> <span style="color: #008080;">33</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> someDbTask()</span><span style="color: #008080;">34</span> <span style="color: #000000;">    {</span><span style="color: #008080;">35</span>         <span style="color: #800080;">$connection</span> = <span style="color: #800080;">$this</span>-><span style="color: #000000;">_connection;</span><span style="color: #008080;">36</span> <span style="color: #008080;">37</span>         <span style="color: #008000;">//</span><span style="color: #008000;"> ...</span><span style="color: #008080;">38</span> <span style="color: #000000;">    }</span><span style="color: #008080;">39</span> <span style="color: #008080;">40</span> <span style="color: #000000;">}</span><span style="color: #008080;">41</span> <span style="color: #008080;">42</span> <span style="color: #800080;">$some</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> SomeComponent();</span><span style="color: #008080;">43</span> <span style="color: #008080;">44</span> <span style="color: #008000;">//</span><span style="color: #008000;">Pass the connection defined in the registry</span><span style="color: #008080;">45</span> <span style="color: #800080;">$some</span>->setConnection(Registry::<span style="color: #000000;">getConnection());</span><span style="color: #008080;">46</span> <span style="color: #008080;">47</span> <span style="color: #800080;">$some</span>->someDbTask();
Copy after login

  现在,让我们来想像一下,我们必须在组件中实现两个方法,首先需要创建一个新的数据库连接,第二个总是获得一个共享连接:

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Registry</span><span style="color: #008080;"> 4</span> <span style="color: #000000;">{</span><span style="color: #008080;"> 5</span> <span style="color: #008080;"> 6</span>     <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$_connection</span><span style="color: #000000;">;</span><span style="color: #008080;"> 7</span> <span style="color: #008080;"> 8</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*</span><span style="color: #008080;"> 9</span> <span style="color: #008000;">     * Creates a connection</span><span style="color: #008080;">10</span>      <span style="color: #008000;">*/</span><span style="color: #008080;">11</span>     <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> _createConnection()</span><span style="color: #008080;">12</span> <span style="color: #000000;">    {</span><span style="color: #008080;">13</span>         <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> Connection(<span style="color: #0000ff;">array</span><span style="color: #000000;">(</span><span style="color: #008080;">14</span>             "host" => "localhost",<span style="color: #008080;">15</span>             "username" => "root",<span style="color: #008080;">16</span>             "password" => "secret",<span style="color: #008080;">17</span>             "dbname" => "invo"<span style="color: #008080;">18</span> <span style="color: #000000;">        ));</span><span style="color: #008080;">19</span> <span style="color: #000000;">    }</span><span style="color: #008080;">20</span> <span style="color: #008080;">21</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*</span><span style="color: #008080;">22</span> <span style="color: #008000;">     * Creates a connection only once and returns it</span><span style="color: #008080;">23</span>      <span style="color: #008000;">*/</span><span style="color: #008080;">24</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getSharedConnection()</span><span style="color: #008080;">25</span> <span style="color: #000000;">    {</span><span style="color: #008080;">26</span>         <span style="color: #0000ff;">if</span> (self::<span style="color: #800080;">$_connection</span>===<span style="color: #0000ff;">null</span><span style="color: #000000;">){</span><span style="color: #008080;">27</span>             <span style="color: #800080;">$connection</span> = self::<span style="color: #000000;">_createConnection();</span><span style="color: #008080;">28</span>             self::<span style="color: #800080;">$_connection</span> = <span style="color: #800080;">$connection</span><span style="color: #000000;">;</span><span style="color: #008080;">29</span> <span style="color: #000000;">        }</span><span style="color: #008080;">30</span>         <span style="color: #0000ff;">return</span> self::<span style="color: #800080;">$_connection</span><span style="color: #000000;">;</span><span style="color: #008080;">31</span> <span style="color: #000000;">    }</span><span style="color: #008080;">32</span> <span style="color: #008080;">33</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*</span><span style="color: #008080;">34</span> <span style="color: #008000;">     * Always returns a new connection</span><span style="color: #008080;">35</span>      <span style="color: #008000;">*/</span><span style="color: #008080;">36</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getNewConnection()</span><span style="color: #008080;">37</span> <span style="color: #000000;">    {</span><span style="color: #008080;">38</span>         <span style="color: #0000ff;">return</span> self::<span style="color: #000000;">_createConnection();</span><span style="color: #008080;">39</span> <span style="color: #000000;">    }</span><span style="color: #008080;">40</span> <span style="color: #008080;">41</span> <span style="color: #000000;">}</span><span style="color: #008080;">42</span> <span style="color: #008080;">43</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> SomeComponent</span><span style="color: #008080;">44</span> <span style="color: #000000;">{</span><span style="color: #008080;">45</span> <span style="color: #008080;">46</span>     <span style="color: #0000ff;">protected</span> <span style="color: #800080;">$_connection</span><span style="color: #000000;">;</span><span style="color: #008080;">47</span> <span style="color: #008080;">48</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*</span><span style="color: #008080;">49</span> <span style="color: #008000;">     * Sets the connection externally</span><span style="color: #008080;">50</span>      <span style="color: #008000;">*/</span><span style="color: #008080;">51</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> setConnection(<span style="color: #800080;">$connection</span><span style="color: #000000;">){</span><span style="color: #008080;">52</span>         <span style="color: #800080;">$this</span>->_connection = <span style="color: #800080;">$connection</span><span style="color: #000000;">;</span><span style="color: #008080;">53</span> <span style="color: #000000;">    }</span><span style="color: #008080;">54</span> <span style="color: #008080;">55</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*</span><span style="color: #008080;">56</span> <span style="color: #008000;">     * This method always needs the shared connection</span><span style="color: #008080;">57</span>      <span style="color: #008000;">*/</span><span style="color: #008080;">58</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> someDbTask()</span><span style="color: #008080;">59</span> <span style="color: #000000;">    {</span><span style="color: #008080;">60</span>         <span style="color: #800080;">$connection</span> = <span style="color: #800080;">$this</span>-><span style="color: #000000;">_connection;</span><span style="color: #008080;">61</span> <span style="color: #008080;">62</span>         <span style="color: #008000;">//</span><span style="color: #008000;"> ...</span><span style="color: #008080;">63</span> <span style="color: #000000;">    }</span><span style="color: #008080;">64</span> <span style="color: #008080;">65</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*</span><span style="color: #008080;">66</span> <span style="color: #008000;">     * This method always needs a new connection</span><span style="color: #008080;">67</span>      <span style="color: #008000;">*/</span><span style="color: #008080;">68</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> someOtherDbTask(<span style="color: #800080;">$connection</span><span style="color: #000000;">)</span><span style="color: #008080;">69</span> <span style="color: #000000;">    {</span><span style="color: #008080;">70</span> <span style="color: #008080;">71</span> <span style="color: #000000;">    }</span><span style="color: #008080;">72</span> <span style="color: #008080;">73</span> <span style="color: #000000;">}</span><span style="color: #008080;">74</span> <span style="color: #008080;">75</span> <span style="color: #800080;">$some</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> SomeComponent();</span><span style="color: #008080;">76</span> <span style="color: #008080;">77</span> <span style="color: #008000;">//</span><span style="color: #008000;">This injects the shared connection</span><span style="color: #008080;">78</span> <span style="color: #800080;">$some</span>->setConnection(Registry::<span style="color: #000000;">getSharedConnection());</span><span style="color: #008080;">79</span> <span style="color: #008080;">80</span> <span style="color: #800080;">$some</span>-><span style="color: #000000;">someDbTask();</span><span style="color: #008080;">81</span> <span style="color: #008080;">82</span> <span style="color: #008000;">//</span><span style="color: #008000;">Here, we always pass a new connection as parameter</span><span style="color: #008080;">83</span> <span style="color: #800080;">$some</span>->someOtherDbTask(Registry::getConnection());
Copy after login

  到此为止,我们已经看到了如何使用依赖注入解决我们的问题。不是在代码内部创建依赖关系,而是让其作为一个参数传递,这使得我们的程序更容易维护,降低程序代码的耦合度,实现一种松耦合。但是从长远来看,这种形式的依赖注入也有一些缺点。

  例如,如果组件中有较多的依赖关系,我们需要创建多个setter方法传递,或创建构造函数进行传递。另外,每次使用组件时,都需要创建依赖组件,使代码维护不太易,我们编写的代码可能像这样:

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #008000;">//</span><span style="color: #008000;">Create the dependencies or retrieve them from the registry</span><span style="color: #008080;"> 4</span> <span style="color: #800080;">$connection</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Connection();</span><span style="color: #008080;"> 5</span> <span style="color: #800080;">$session</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Session();</span><span style="color: #008080;"> 6</span> <span style="color: #800080;">$fileSystem</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> FileSystem();</span><span style="color: #008080;"> 7</span> <span style="color: #800080;">$filter</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Filter();</span><span style="color: #008080;"> 8</span> <span style="color: #800080;">$selector</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Selector();</span><span style="color: #008080;"> 9</span> <span style="color: #008080;">10</span> <span style="color: #008000;">//</span><span style="color: #008000;">Pass them as constructor parameters</span><span style="color: #008080;">11</span> <span style="color: #800080;">$some</span> = <span style="color: #0000ff;">new</span> SomeComponent(<span style="color: #800080;">$connection</span>, <span style="color: #800080;">$session</span>, <span style="color: #800080;">$fileSystem</span>, <span style="color: #800080;">$filter</span>, <span style="color: #800080;">$selector</span><span style="color: #000000;">);</span><span style="color: #008080;">12</span> <span style="color: #008080;">13</span> <span style="color: #008000;">//</span><span style="color: #008000;"> ... or using setters</span><span style="color: #008080;">14</span> <span style="color: #008080;">15</span> <span style="color: #800080;">$some</span>->setConnection(<span style="color: #800080;">$connection</span><span style="color: #000000;">);</span><span style="color: #008080;">16</span> <span style="color: #800080;">$some</span>->setSession(<span style="color: #800080;">$session</span><span style="color: #000000;">);</span><span style="color: #008080;">17</span> <span style="color: #800080;">$some</span>->setFileSystem(<span style="color: #800080;">$fileSystem</span><span style="color: #000000;">);</span><span style="color: #008080;">18</span> <span style="color: #800080;">$some</span>->setFilter(<span style="color: #800080;">$filter</span><span style="color: #000000;">);</span><span style="color: #008080;">19</span> <span style="color: #800080;">$some</span>->setSelector(<span style="color: #800080;">$selector</span>);
Copy after login

  我想,我们不得不在应用程序的许多地方创建这个对象。如果你不需要依赖的组件后,我们又要去代码注入部分移除构造函数中的参数或者是setter方法。为了解决这个问题,我们再次返回去使用一个全局注册表来创建组件。但是,在创建对象之前,它增加了一个新的抽象层:

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> SomeComponent</span><span style="color: #008080;"> 4</span> <span style="color: #000000;">{</span><span style="color: #008080;"> 5</span> <span style="color: #008080;"> 6</span>     <span style="color: #008000;">//</span><span style="color: #008000;"> ...</span><span style="color: #008080;"> 7</span> <span style="color: #008080;"> 8</span>     <span style="color: #008000;">/*</span><span style="color: #008000;">*</span><span style="color: #008080;"> 9</span> <span style="color: #008000;">     * Define a factory method to create SomeComponent instances injecting its dependencies</span><span style="color: #008080;">10</span>      <span style="color: #008000;">*/</span><span style="color: #008080;">11</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> factory()</span><span style="color: #008080;">12</span> <span style="color: #000000;">    {</span><span style="color: #008080;">13</span> <span style="color: #008080;">14</span>         <span style="color: #800080;">$connection</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Connection();</span><span style="color: #008080;">15</span>         <span style="color: #800080;">$session</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Session();</span><span style="color: #008080;">16</span>         <span style="color: #800080;">$fileSystem</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> FileSystem();</span><span style="color: #008080;">17</span>         <span style="color: #800080;">$filter</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Filter();</span><span style="color: #008080;">18</span>         <span style="color: #800080;">$selector</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Selector();</span><span style="color: #008080;">19</span> <span style="color: #008080;">20</span>         <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> self(<span style="color: #800080;">$connection</span>, <span style="color: #800080;">$session</span>, <span style="color: #800080;">$fileSystem</span>, <span style="color: #800080;">$filter</span>, <span style="color: #800080;">$selector</span><span style="color: #000000;">);</span><span style="color: #008080;">21</span> <span style="color: #000000;">    }</span><span style="color: #008080;">22</span> <span style="color: #008080;">23</span> }
Copy after login

  这一刻,我们好像回到了问题的开始,我们正在创建组件内部的依赖,我们每次都在修改以及找寻一种解决问题的办法,但这都不是很好的做法。

  一种实用和优雅的来解决这些问题,是使用容器的依赖注入,像我们在前面看到的,容器作为全局注册表,使用容器的依赖注入做为一种桥梁来解决依赖可以使我们的代码耦合度更低,很好的降低了组件的复杂性:

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> SomeComponent</span><span style="color: #008080;"> 4</span> <span style="color: #000000;">{</span><span style="color: #008080;"> 5</span> <span style="color: #008080;"> 6</span>     <span style="color: #0000ff;">protected</span> <span style="color: #800080;">$_di</span><span style="color: #000000;">;</span><span style="color: #008080;"> 7</span> <span style="color: #008080;"> 8</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$di</span><span style="color: #000000;">)</span><span style="color: #008080;"> 9</span> <span style="color: #000000;">    {</span><span style="color: #008080;">10</span>         <span style="color: #800080;">$this</span>->_di = <span style="color: #800080;">$di</span><span style="color: #000000;">;</span><span style="color: #008080;">11</span> <span style="color: #000000;">    }</span><span style="color: #008080;">12</span> <span style="color: #008080;">13</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> someDbTask()</span><span style="color: #008080;">14</span> <span style="color: #000000;">    {</span><span style="color: #008080;">15</span> <span style="color: #008080;">16</span>         <span style="color: #008000;">//</span><span style="color: #008000;"> Get the connection service</span><span style="color: #008080;">17</span> <span style="color: #008000;">        // Always returns a new connection</span><span style="color: #008080;">18</span>         <span style="color: #800080;">$connection</span> = <span style="color: #800080;">$this</span>->_di->get('db'<span style="color: #000000;">);</span><span style="color: #008080;">19</span> <span style="color: #008080;">20</span> <span style="color: #000000;">    }</span><span style="color: #008080;">21</span> <span style="color: #008080;">22</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> someOtherDbTask()</span><span style="color: #008080;">23</span> <span style="color: #000000;">    {</span><span style="color: #008080;">24</span> <span style="color: #008080;">25</span>         <span style="color: #008000;">//</span><span style="color: #008000;"> Get a shared connection service,</span><span style="color: #008080;">26</span> <span style="color: #008000;">        // this will return the same connection everytime</span><span style="color: #008080;">27</span>         <span style="color: #800080;">$connection</span> = <span style="color: #800080;">$this</span>->_di->getShared('db'<span style="color: #000000;">);</span><span style="color: #008080;">28</span> <span style="color: #008080;">29</span>         <span style="color: #008000;">//</span><span style="color: #008000;">This method also requires a input filtering service</span><span style="color: #008080;">30</span>         <span style="color: #800080;">$filter</span> = <span style="color: #800080;">$this</span>->_db->get('filter'<span style="color: #000000;">);</span><span style="color: #008080;">31</span> <span style="color: #008080;">32</span> <span style="color: #000000;">    }</span><span style="color: #008080;">33</span> <span style="color: #008080;">34</span> <span style="color: #000000;">}</span><span style="color: #008080;">35</span> <span style="color: #008080;">36</span> <span style="color: #800080;">$di</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Phalcon\DI();</span><span style="color: #008080;">37</span> <span style="color: #008080;">38</span> <span style="color: #008000;">//</span><span style="color: #008000;">Register a "db" service in the container</span><span style="color: #008080;">39</span> <span style="color: #800080;">$di</span>->set('db', <span style="color: #0000ff;">function</span><span style="color: #000000;">(){</span><span style="color: #008080;">40</span>     <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> Connection(<span style="color: #0000ff;">array</span><span style="color: #000000;">(</span><span style="color: #008080;">41</span>         "host" => "localhost",<span style="color: #008080;">42</span>         "username" => "root",<span style="color: #008080;">43</span>         "password" => "secret",<span style="color: #008080;">44</span>         "dbname" => "invo"<span style="color: #008080;">45</span> <span style="color: #000000;">    ));</span><span style="color: #008080;">46</span> <span style="color: #000000;">});</span><span style="color: #008080;">47</span> <span style="color: #008080;">48</span> <span style="color: #008000;">//</span><span style="color: #008000;">Register a "filter" service in the container</span><span style="color: #008080;">49</span> <span style="color: #800080;">$di</span>->set('filter', <span style="color: #0000ff;">function</span><span style="color: #000000;">(){</span><span style="color: #008080;">50</span>     <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span><span style="color: #000000;"> Filter();</span><span style="color: #008080;">51</span> <span style="color: #000000;">});</span><span style="color: #008080;">52</span> <span style="color: #008080;">53</span> <span style="color: #008000;">//</span><span style="color: #008000;">Register a "session" service in the container</span><span style="color: #008080;">54</span> <span style="color: #800080;">$di</span>->set('session', <span style="color: #0000ff;">function</span><span style="color: #000000;">(){</span><span style="color: #008080;">55</span>     <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span><span style="color: #000000;"> Session();</span><span style="color: #008080;">56</span> <span style="color: #000000;">});</span><span style="color: #008080;">57</span> <span style="color: #008080;">58</span> <span style="color: #008000;">//</span><span style="color: #008000;">Pass the service container as unique parameter</span><span style="color: #008080;">59</span> <span style="color: #800080;">$some</span> = <span style="color: #0000ff;">new</span> SomeComponent(<span style="color: #800080;">$di</span><span style="color: #000000;">);</span><span style="color: #008080;">60</span> <span style="color: #008080;">61</span> <span style="color: #800080;">$some</span>->someTask();
Copy after login

现在,该组件只有访问某种service的时候才需要它,如果它不需要,它甚至不初始化,以节约资源。该组件是高度解耦。他们的行为,或者说他们的任何其他方面都不会影响到组件本身。
我们的实现办法¶

Phalcon\DI 是一个实现了服务的依赖注入功能的组件,它本身也是一个容器。

由于Phalcon高度解耦,Phalcon\DI 是框架用来集成其他组件的必不可少的部分,开发人员也可以使用这个组件依赖注入和管理应用程序中不同类文件的实例。

基本上,这个组件实现了 Inversion of Control 模式。基于此,对象不再以构造函数接收参数或者使用setter的方式来实现注入,而是直接请求服务的依赖注入。这就大大降低了整体程序的复杂性,因为只有一个方法用以获得所需要的一个组件的依赖关系。

此外,这种模式增强了代码的可测试性,从而使它不容易出错。
在容器中注册服务¶

框架本身或开发人员都可以注册服务。当一个组件A要求调用组件B(或它的类的一个实例),可以从容器中请求调用组件B,而不是创建组件B的一个实例。

这种工作方式为我们提供了许多优点:

我们可以更换一个组件,从他们本身或者第三方轻松创建。
在组件发布之前,我们可以充分的控制对象的初始化,并对对象进行各种设置。
我们可以使用统一的方式从组件得到一个结构化的全局实例

服务可以通过以下几种方式注入到容器:

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #008000;">//</span><span style="color: #008000;">Create the Dependency Injector Container</span><span style="color: #008080;"> 4</span> <span style="color: #800080;">$di</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Phalcon\DI();</span><span style="color: #008080;"> 5</span> <span style="color: #008080;"> 6</span> <span style="color: #008000;">//</span><span style="color: #008000;">By its class name</span><span style="color: #008080;"> 7</span> <span style="color: #800080;">$di</span>->set("request", 'Phalcon\Http\Request'<span style="color: #000000;">);</span><span style="color: #008080;"> 8</span> <span style="color: #008080;"> 9</span> <span style="color: #008000;">//</span><span style="color: #008000;">Using an anonymous function, the instance will lazy loaded</span><span style="color: #008080;">10</span> <span style="color: #800080;">$di</span>->set("request", <span style="color: #0000ff;">function</span><span style="color: #000000;">(){</span><span style="color: #008080;">11</span>     <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span><span style="color: #000000;"> Phalcon\Http\Request();</span><span style="color: #008080;">12</span> <span style="color: #000000;">});</span><span style="color: #008080;">13</span> <span style="color: #008080;">14</span> <span style="color: #008000;">//</span><span style="color: #008000;">Registering directly an instance</span><span style="color: #008080;">15</span> <span style="color: #800080;">$di</span>->set("request", <span style="color: #0000ff;">new</span><span style="color: #000000;"> Phalcon\Http\Request());</span><span style="color: #008080;">16</span> <span style="color: #008080;">17</span> <span style="color: #008000;">//</span><span style="color: #008000;">Using an array definition</span><span style="color: #008080;">18</span> <span style="color: #800080;">$di</span>->set("request", <span style="color: #0000ff;">array</span><span style="color: #000000;">(</span><span style="color: #008080;">19</span>     "className" => 'Phalcon\Http\Request'<span style="color: #008080;">20</span> ));
Copy after login

在上面的例子中,当向框架请求访问一个请求数据时,它将首先确定容器中是否存在这个”reqeust”名称的服务。

容器会反回一个请求数据的实例,开发人员最终得到他们想要的组件。

在上面示例中的每一种方法都有优缺点,具体使用哪一种,由开发过程中的特定场景来决定的。

用一个字符串来设定一个服务非常简单,但缺少灵活性。设置服务时,使用数组则提供了更多的灵活性,而且可以使用较复杂的代码。lambda函数是两者之间一个很好的平衡,但也可能导致更多的维护管理成本。

Phalcon\DI 提供服务的延迟加载。除非开发人员在注入服务的时候直接实例化一个对象,然后存存储到容器中。在容器中,通过数组,字符串等方式存储的服务都将被延迟加载,即只有在请求对象的时候才被初始化。

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #008000;">//</span><span style="color: #008000;">Register a service "db" with a class name and its parameters</span><span style="color: #008080;"> 4</span> <span style="color: #800080;">$di</span>->set("db", <span style="color: #0000ff;">array</span><span style="color: #000000;">(</span><span style="color: #008080;"> 5</span>     "className" => "Phalcon\Db\Adapter\Pdo\Mysql",<span style="color: #008080;"> 6</span>     "parameters" => <span style="color: #0000ff;">array</span><span style="color: #000000;">(</span><span style="color: #008080;"> 7</span>           "parameter" => <span style="color: #0000ff;">array</span><span style="color: #000000;">(</span><span style="color: #008080;"> 8</span>                "host" => "localhost",<span style="color: #008080;"> 9</span>                "username" => "root",<span style="color: #008080;">10</span>                "password" => "secret",<span style="color: #008080;">11</span>                "dbname" => "blog"<span style="color: #008080;">12</span> <span style="color: #000000;">          )</span><span style="color: #008080;">13</span> <span style="color: #000000;">    )</span><span style="color: #008080;">14</span> <span style="color: #000000;">));</span><span style="color: #008080;">15</span> <span style="color: #008080;">16</span> <span style="color: #008000;">//</span><span style="color: #008000;">Using an anonymous function</span><span style="color: #008080;">17</span> <span style="color: #800080;">$di</span>->set("db", <span style="color: #0000ff;">function</span><span style="color: #000000;">(){</span><span style="color: #008080;">18</span>     <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> Phalcon\Db\Adapter\Pdo\<span style="color: #008080;">Mysql</span>(<span style="color: #0000ff;">array</span><span style="color: #000000;">(</span><span style="color: #008080;">19</span>          "host" => "localhost",<span style="color: #008080;">20</span>          "username" => "root",<span style="color: #008080;">21</span>          "password" => "secret",<span style="color: #008080;">22</span>          "dbname" => "blog"<span style="color: #008080;">23</span> <span style="color: #000000;">    ));</span><span style="color: #008080;">24</span> });
Copy after login

以上这两种服务的注册方式产生相同的结果。然后,通过数组定义的,在后面需要的时候,你可以修改服务参数:

<span style="color: #008080;">1</span> <span style="color: #000000;">php</span><span style="color: #008080;">2</span> <span style="color: #008080;">3</span> <span style="color: #800080;">$di</span>->setParameter("db", 0, <span style="color: #0000ff;">array</span><span style="color: #000000;">(</span><span style="color: #008080;">4</span>     "host" => "localhost",<span style="color: #008080;">5</span>     "username" => "root",<span style="color: #008080;">6</span>     "password" => "secret"<span style="color: #008080;">7</span> ));
Copy after login

从容器中获得服务的最简单方式就是使用”get”方法,它将从容器中返回一个新的实例:

<span style="color: #008080;">1</span> <span style="color: #000000;">php</span><span style="color: #008080;">2</span>      <span style="color: #800080;">$request</span> = <span style="color: #800080;">$di</span>->get("request");
Copy after login

或者通过下面这种魔术方法的形式调用:

<span style="color: #008080;">1</span> <span style="color: #000000;">php</span><span style="color: #008080;">2</span> <span style="color: #008080;">3</span> <span style="color: #800080;">$request</span> = <span style="color: #800080;">$di</span>-><span style="color: #000000;">getRequest();</span><span style="color: #008080;">4</span> <span style="color: #008080;">5</span> Phalcon\DI 同时允许服务重用,为了得到一个已经实例化过的服务,可以使用 getShared() 方法的形式来获得服务。
Copy after login

具体的 Phalcon\Http\Request 请求示例:

<span style="color: #008080;">1</span> <span style="color: #000000;">php</span><span style="color: #008080;">2</span> <span style="color: #008080;">3</span> <span style="color: #800080;">$request</span> = <span style="color: #800080;">$di</span>->getShared("request");
Copy after login

参数还可以在请求的时候通过将一个数组参数传递给构造函数的方式:

<span style="color: #008080;">1</span> <span style="color: #000000;">php</span><span style="color: #008080;">2</span> <span style="color: #008080;">3</span> <span style="color: #800080;">$component</span> = <span style="color: #800080;">$di</span>->get("MyComponent", <span style="color: #0000ff;">array</span>("some-parameter", "other"))
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template