• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

对PHP依赖注入的理解实例分析

php 搞代码 4年前 (2022-01-22) 43次浏览 已收录 0个评论

本文实例讲述了对PHP依赖注入的理解。分享给大家供大家参考,具体如下:

看Laravel的IoC容器文档只是介绍实例,但是没有说原理,之前用MVC框架都没有在意这个概念,无意中在phalcon的文档中看到这个详细的介绍,感觉豁然开朗,复制粘贴过来,主要是好久没有写东西了,现在确实很懒变得!

首先,我们假设,我们要开发一个组件命名为SomeComponent。这个组件中现在将要注入一个数据库连接。

在这个例子中,数据库连接在component中被创建,这种方法是不切实际的,这样做的话,我们将不能改变数据库连接参数及数据库类型等一些参数。

class SomeComponent {  /**   * The instantiation of the connection is hardcoded inside   * the component so is difficult to replace it externally   * or change its behavior   */  public function someDbTask()  {    $connection = new Connection(array(      "host" => "localhost",      "username" => "root",      "password" => "secret",      "dbname" => "invo"    ));    // ...  }}$some = new SomeComponent();$some->someDbTask();

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

class SomeComponent {  protected $_connection;  /**   * Sets the connection externally   */  public function setConnection($connection)  {    $this->_connection = $connection;  }  public function someDbTask()  {    $connection = $this->_connection;    // ...  }}$some = new SomeComponent();//Create the connection$connection = new Connection(array(  "host" => "localhost",  "username" => "root",  "password" => "secret",  "dbname" => "invo"));//Inject the connection in the component$some->setConnection($connection);$some->someDbTask();

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

class Registry{  /**   * Returns the connection   */  public static function getConnection()  {    return new Connection(array(      "host" => "localhost",      "username" => "root",      "password" => "secret",      "dbname" => "invo"    ));  }}class SomeComponent{  protected $_connection;  /**   * Sets the connection externally   */  public function setConnection($connection){    $this->_connection = $connection;  }  public function someDbTask()  {    $connection = $this->_connection;    // ...  }}$some = new SomeComponent();//Pass the connection defined in the registry$some->setConnection(Registry::getConnection());$some->someDbTask();

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

class Registry{  protected static $_connection;  /**   * Creates a connect<a>2本文来源gao*daima.com搞@代#码&网6</a><pre>搞gaodaima代码

ion */ protected static function _createConnection() { return new Connection(array( "host" => "localhost", "username" => "root", "password" => "secret", "dbname" => "invo" )); } /** * Creates a connection only once and returns it */ public static function getSharedConnection() { if (self::$_connection===null){ $connection = self::_createConnection(); self::$_connection = $connection; } return self::$_connection; } /** * Always returns a new connection */ public static function getNewConnection() { return self::_createConnection(); }}class SomeComponent{ protected $_connection; /** * Sets the connection externally */ public function setConnection($connection){ $this->_connection = $connection; } /** * This method always needs the shared connection */ public function someDbTask() { $connection = $this->_connection; // … } /** * This method always needs a new connection */ public function someOtherDbTask($connection) { }}$some = new SomeComponent();//This injects the shared connection$some->setConnection(Registry::getSharedConnection());$some->someDbTask();//Here, we always pass a new connection as parameter$some->someOtherDbTask(Registry::getConnection());


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:对PHP依赖注入的理解实例分析

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址