依赖注入模式需要在调用者外部完成容器创建以及容器中接口与实现类的运行时绑定工作,在 Laravel 中该容器就是服务容器,而接口与实现类的运行时绑定则在服务提供者中完成。此外,除了在调用者的构造函数中进行依赖注入外,还可以通过在调用者的方法中进行依赖注入。
三种常见注入方式:
- 构造注入
- setter注入
- 接口注入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| interface Shape { public function getCircum();
public function getArea(); }
class Rectangle implements Shape {
private $width, $height;
public function __construct($width, $height) { $this->width = $width; $this->height = $height; }
public function getCircum() {
return ($this->width + $this->height) * 2; }
public function getArea() {
return $this->width * $this->height; } }
class Circle implements Shape {
private $radii;
public function __construct($radii) { $this->radii = $radii; }
public function getCircum() { return 2 * M_PI * $this->radii;
}
public function getArea() { return M_PI * pow($this->radii, 2); } }
|
构造注入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class DependencyInjection { public $shape;
public function __construct(Shape $shape) { $this->shape = $shape; } }
$rectangle = new Rectangle(4, 2); $r_di = new DependencyInjection($rectangle); echo $r_di->shape->getCircum(); echo $r_di->shape->getArea();
$circle = new Circle(3); $c_di = new DependencyInjection($circle); echo $c_di->shape->getCircum(); echo $c_di->shape->getArea();
|
setter 注入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class DependencyInjectionSetter {
public $shape;
public function setter(Shape $shape) {
$this->shape = $shape; } }
$rectangle = new Rectangle(4, 2); $setter_di = new DependencyInjectionSetter(); $setter_di->setter($rectangle); echo $setter_di->shape->getCircum();
|
interface 注入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| interface ServiceSetter { public function setService(Shape $shape); }
class ServiceClient implements ServiceSetter {
public $shape;
public function setService(Shape $shape) { $this->shape = $shape; } }
$sc = new ServiceClient(); $sc->setService($rectangle); echo $sc->shape->getArea();
|