PHP 依赖注入及解读

//依赖注入是应用于一个类的实例化需要依赖另外一个类的场景
//Person依赖于Student类,Student类注入到Person

  class Person{
    public function teach($obj){
      return $obj->study();
    }
  }

  class Student{
    public function study(){
      return 123;
    }
  }

  public function personTest(){
    $p = new Person();
    $s = new Student();
    echo $p->teach($s);
  }

posted @ 2020-08-28 14:43  爱搬砖的小码农  阅读(270)  评论(0编辑  收藏  举报