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();