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