abstract:class Father{ public function output(){ return "hello world"; } } //
class Father{ public function output(){ return "hello world"; } } //使用構(gòu)造方法實(shí)現(xiàn)依賴注入 class Son{ private $father = null;//制造容器來儲存 public function __construct(Father $father) { $this -> father = $father;//把大類傳入到小類中保存起來了 } public function getInfo() { return $this->father->output(); } //在函數(shù)中進(jìn)行使用 $father = new Father; $son = new Son($father); echo $son->output();
Correcting teacher:天蓬老師Correction time:2019-04-10 13:28:48
Teacher's summary:依賴注入, 簡單的講, 就是把外部對象通過方法參數(shù)的形式傳入, 而不是方法中直接實(shí)例化, 以提升方法的獨(dú)立性