php设计模式-简单依赖注入
<?php class A { private $_b; public function __construct($b) { $this->_b = $b; } public function sayHello() { echo 'I am A'; $this->_b->sayHello(); } } class B { private $_c; public function __construct($c) { $this->_c = $c; } public function sayHello() { echo 'I am B'; $this->_c->sayHello(); } } class C { public function sayHello() { echo 'I am C'; } } $a = new A(new B(new C())); $a->sayHello();
运行结果:
I am C I am B I am A