abstrait:class Father{ public function output(){ return "hello world"; } } //
class Father{ public function output(){ return "hello world"; } } //使用構(gòu)造方法實(shí)現(xiàn)依賴(lài)注入 class Son{ private $father = null;//制造容器來(lái)儲(chǔ)存 public function __construct(Father $father) { $this -> father = $father;//把大類(lèi)傳入到小類(lèi)中保存起來(lái)了 } public function getInfo() { return $this->father->output(); } //在函數(shù)中進(jìn)行使用 $father = new Father; $son = new Son($father); echo $son->output();
Professeur correcteur:天蓬老師Temps de correction:2019-04-10 13:28:48
Résumé du professeur:依賴(lài)注入, 簡(jiǎn)單的講, 就是把外部對(duì)象通過(guò)方法參數(shù)的形式傳入, 而不是方法中直接實(shí)例化, 以提升方法的獨(dú)立性