首页 > 编程语言 >php中的依赖注入模式

php中的依赖注入模式

时间:2022-11-17 12:39:02浏览次数:34  
标签:function 依赖 string private 调用者 configuration php public 注入



概念

 

依赖注入模式(Dependency Injection),用松散耦合的方式来更好的实现可测试、可维护和可扩展的代码。
依赖注入模式是控制反转(Inversion of Control)的一种实现方式。

要实现控制反转,通常的解决方案是将创建被调用者实例的工作交由 IoC 容器来完成,然后在调用者中注入被调用者(通过构造器 / 方法注入实现),这样我们就实现了调用者与被调用者的解耦,该过程被称为依赖注入。

 

示例


<?php


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;
}
}


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()
);
}
}

$config = new DatabaseConfiguration('localhost', 3306, 'root', 'root');
$connection = new DatabaseConnection($config);


 

标签:function,依赖,string,private,调用者,configuration,php,public,注入
From: https://blog.51cto.com/u_6353447/5860631

相关文章

  • php中的状态模式
    概念 状态模式当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。状态模式主要解决的是当控制一个对象状态的条件表达式过于复杂时的情况。把状态的......
  • php中介者模式
    概念 中介者模式用于开发一个对象,这个对象能够在类似对象相互之间不直接相互的情况下传送或者调解对这些对象的集合的修改。一般处理具有类似属性,需要保持同步的非耦合对象......
  • the real ugly parts of PHP (ZZ)
    来源:https://www.amazon.com/gp/customer-reviews/R2D9T7KNTHFC5A IshouldwarnthereaderthatIneverwasaPHPenthousiastinspiteofbeingable,though,to......
  • php 反序列化字符串逃逸
    这里总结一下反序列化字符串逃逸的知识点反序列化字符串逃逸分为被过滤后字符增多和字符减少的情况这里就不讲之前的基础知识了大家看其它师傅写的博客就可以了很多师......
  • CTFshow刷题日记-WEB-PHP特性(下篇123-150)
    web123,125,126error_reporting(0);highlight_file(__FILE__);include("flag.php");$a=$_SERVER['argv'];$c=$_POST['fun'];if(isset($_POST['CTF_SHOW'])&&isset($_POST['C......
  • 010005 PHP 第一个计算长方体的体积公式
    <?phpheader('Content-Type:text/html;charset=utf-8');include'./assets/php/head.php';/***第一个计算长方体的体积公式*已知长是40-10,宽是30+20,高是35*2......
  • php面向对象 final关键字
    //final修饰的类无法被子类继承finalclasshuman{}classmanextendshuman{}$m=newman();//Fatalerror:Classmanmaynotinheritfromfinalclass(human)//......
  • php的几种输出函数
    php输出函数函数名功能描述echo()输出字符串print()输出一个或多个字符串print_r()打印关于变量的易于理解的信息printf()输出格式化字符串sprintf()......
  • linux系列---【yum下载软件安装包及其依赖】
    yum下载软件安装包及其依赖1.场景生产中一般都是离线环境,需要安装一些软件的时候非常不方便,有时候下载好了软件,但是由于依赖的环境没安装而导致失败,为了解决这个问题......
  • apache启动遇到phpinfo只显示源码问题
    在安装php和apache的时候,会遇到只显示源码的问题网上找了好多帖子都是在改php.ini的东西,但是改了半天也不对,发现我安装的wordpress目录也打不开,所以我认为这就是apache服......