摘要:class People{
private $cookie;
public function __construct(Cookie $cookie){ //再控制器中填写依赖注入参数
$this->cookie = $cookie;
}
public function eat(){&nbclass People{
private $cookie;
public function __construct(Cookie $cookie){ //再控制器中填写依赖注入参数
$this->cookie = $cookie;
}
public function eat(){ //也可以在eat()方法中填写依赖注入参数
return '人们喜欢吃'.$this->cookie->color();
}
}
class Cookie{
public function color()
{
return '巧克力色的甜甜圈';
}
}
$cookie = new Cookie;
$people = new People($cookie);
echo $people->eat();
依赖注入的优势:
再外部实例化对象,解决了耦合度太高的弊端。
批改老师:查无此人批改时间:2019-03-16 09:33:42
老师总结:现在依赖注入用的越来越多,新技术都要玩熟,才不掉队。继续加油