摘要:class Father{ public function output(){ return "hello world"; } } //
class Father{
public function output(){
return "hello world";
}
}
//使用构造方法实现依赖注入
class Son{
private $father = null;//制造容器来储存
public function __construct(Father $father)
{
$this -> father = $father;//把大类传入到小类中保存起来了
}
public function getInfo()
{
return $this->father->output();
}
//在函数中进行使用
$father = new Father;
$son = new Son($father);
echo $son->output();
批改老师:天蓬老师批改时间:2019-04-10 13:28:48
老师总结:依赖注入, 简单的讲, 就是把外部对象通过方法参数的形式传入, 而不是方法中直接实例化, 以提升方法的独立性