依赖注入案例

Original 2019-04-10 12:40:57 207
abstract: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();


Correcting teacher:天蓬老师Correction time:2019-04-10 13:28:48
Teacher's summary:依赖注入, 简单的讲, 就是把外部对象通过方法参数的形式传入, 而不是方法中直接实例化, 以提升方法的独立性

Release Notes

Popular Entries