详解PHP设计模式之依赖注入模式

吾爱主题 阅读:143 2021-11-15 15:56:00 评论:0

目的

实现了松耦合的软件架构,可得到更好的测试,管理和扩展的代码

用法

DatabaseConfiguration 被注入 DatabaseConnection 并获取所需的 $config 。如果没有依赖注入模式, 配置将直接创建 DatabaseConnection 。这对测试和扩展来说很不好。

例子

Doctrine2 ORM 使用依赖注入。 例如,注入到 Connection 对象的配置。 对于测试而言, 可以轻松的创建可扩展的模拟数据并注入到 Connection 对象中。

Symfony 和 Zend Framework 2 已经有了依赖注入的容器。他们通过配置的数组来创建对象,并在需要的地方注入 (在控制器中)。

UML 图

代码DatabaseConfiguration.php

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 <?php   namespace DesignPatterns\Structural\DependencyInjection;   class DatabaseConfiguration {      /**       * @var string       */      private $host ;        /**       * @var int       */      private $port ;        /**       * @var string       */      private $username ;        /**       * @var string       */      private $password ;        public function __construct(string $host , int $port , string $username , string $password )      {          $this ->host = $host ;          $this ->port = $port ;          $this ->username = $username ;          $this ->password = $password ;      }        public function getHost(): string      {          return $this ->host;      }        public function getPort(): int      {          return $this ->port;      }        public function getUsername(): string      {          return $this ->username;      }        public function getPassword(): string      {          return $this ->password;      } } ?>

DatabaseConnection.php

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 <?php   namespace DesignPatterns\Structural\DependencyInjection;   class DatabaseConnection {      /**       * @var DatabaseConfiguration       */      private $configuration ;        /**       * @param DatabaseConfiguration $config       */      public function __construct(DatabaseConfiguration $config )      {          $this ->configuration = $config ;      }        public function getDsn(): string      {          // 这仅仅是演示,而不是一个真正的  DSN          // 注意,这里只使用了注入的配置。 所以,          // 这里是关键的分离关注点。            return sprintf(              '%s:%s@%s:%d' ,              $this ->configuration->getUsername(),              $this ->configuration->getPassword(),              $this ->configuration->getHost(),              $this ->configuration->getPort()          );      } } ?>

测试Tests/DependencyInjectionTest.php

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <?php   namespace DesignPatterns\Structural\DependencyInjection\Tests;   use DesignPatterns\Structural\DependencyInjection\DatabaseConfiguration; use DesignPatterns\Structural\DependencyInjection\DatabaseConnection; use PHPUnit\Framework\TestCase;   class DependencyInjectionTest extends TestCase {      public function testDependencyInjection()      {          $config = new DatabaseConfiguration( 'localhost' , 3306, 'domnikl' , '1234' );          $connection = new DatabaseConnection( $config );            $this ->assertEquals( 'domnikl:1234@localhost:3306' , $connection ->getDsn());      } } ?>

 

以上就是详解PHP设计模式之依赖注入模式的详细内容,更多关于PHP设计模式之依赖注入模式的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/phpyu/p/13675495.html

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容