摘要:<?php class Shop { public function today() { return "不打折";
<?php
class Shop
{
public function today()
{
return "不打折";
}
public function yesterday()
{
return "打9折";
}
}
class Myshop
{
private $shop = null;
public function __construct(Shop $shop)
{
$this->shop = $shop;
}
public function message()
{
return "别人家的商店:".$this->shop->today();
}
}
$shop = new Shop;
$message = new Myshop($shop);
echo $message->message();
解除类与类之间高度的耦合性,降低类与类之间的依赖关系
当代码需要变动的时候,使用依赖注入可以减小工作量,减少需要修改的地方
批改老师:天蓬老师批改时间:2019-08-22 15:34:49
老师总结:依赖注入的本质就是将外部依赖对象通过参数注入到当前对象中, 外部对象的实例化是个重点, 为了它, 大家想尽了办法, 例如工厂模式, 抽象工厂, 容器等, 目前主流是采用服务容器来管理对象以及对象之间的依赖关系, 这和java非常类似