直接上例子:git
這不是依賴注入!程序員
//這不是依賴注入!!! class Bar { } class Foo { protected $bar; public function __construct() { $this->bar = new Bar(); } public function getBar() { return $this->bar; } } $foo = new Foo();
這就是依賴注入github
//這就是依賴注入。。。 class Bar { } class Foo { protected $bar; public function __construct(Bar $bar) { $this->bar = $bar; } public function getBar() { return $this->bar; } } $bar = new Bar(); $foo = new Foo($bar);
這也是依賴注入框架
//這也是依賴注入。。。 class Bar { } class Foo { protected $bar; public function __construct() { } public function setBar(Bar $bar) { $this->bar = $bar; } public function getBar() { return $this->bar; } } $bar = new Bar(); $foo = new Foo(); $foo->setBar($bar);
依賴注入就是new好了依賴的對象注入
進去,而不是在類中顯式的new一個依賴的對象。其實,就是這麼簡單。。。this
雖然思想簡單,可是在下降耦合度方面做用巨大。code
下面舉個例子說明(just for demonstration):
好比咱們作了個小遊戲,裏面的男人能夠親本身的妻子。對象
abstract class Human { } class Woman extends Human { } class Man extends Human { protected $wife; public function __construct() { $this->wife = new Woman(); } public function kissWife() { echo "the man kissed his wife"; } } $man = new Man(); $man->kissWife();
玩的人越來也多,新需求隨之而來。。。繼承
產品經理(
腐腐
):妻子改爲能夠是男的吧,好多用戶有這個需求,這樣玩咱們遊戲的確定更多。
程序員(猿猿
)內心:擦,Wife又能夠是Man,又能夠是Woman,這可咋整。接口
這個時候,依賴注入就能夠閃亮登場了。遊戲
abstract class Human { } class Woman extends Human { } class Man extends Human { protected $wife; public function setWife(Human $human) { $this->wife = $human; } public function kissWife() { echo "the man kissed his wife"; } } $man = new Man(); $man->setWife(new Woman()); $man->kissWife(); $anotherMan = new Man(); $anotherMan->setWife(new Man()); $anotherMan->kissWife();
這裏咱們看到,依賴注入的能夠是繼承依賴類的任何類,因此如今Man的Wife既能夠是Woman也能夠是Man。
玩的人越來也多,新需求隨之而來。。。
產品經理(
宅宅
):把妻子改爲伴侶吧,伴侶裏面除了Man和Woman再加個Cat,好多用戶有這個需求,這樣玩咱們遊戲的確定更多。
程序員(猿猿
)內心:擦,又是Man又是Woman還有Cat,幸虧我會依賴注入。
abstract class Human { } interface canBePartner { } class Cat implements canBePartner { } class Woman extends Human implements canBePartner { } class Man extends Human implements canBePartner { protected $partner; public function setPartner(canBePartner $partner) { $this->partner = $partner; } public function kissPartner() { echo "the man kissed his partner"; } } $man = new Man(); $man->setPartner(new Woman()); $man->kissPartner(); $man2 = new Man(); $man2->setPartner(new Man()); $man2->kissPartner(); $man3 = new Man(); $man3->setPartner(new Cat()); $man3->kissPartner();
這裏咱們看到,依賴注入不但能夠是繼承依賴類的全部子類,也能夠是實現依賴接口的全部類。
因此若是咱們在伴侶中再加入一個Dog,只須要讓它實現canBePartner接口就能夠了:
class Dog implements canBePartner { } $man = new Man(); $man->setPartner(new Dog());
依賴注入雖然下降了耦合度,可是也有缺點,就是須要咱們本身管理注入的對象。
因此,在實際應用中,咱們一般須要實現一個容器去管理和實現依賴對象的注入。
實際上,PHP的經常使用Web框架中都是這麼作的。
博客地址:http://haitian299.github.io/2016/05/17/Dependency-injection/