当前位置:首页 > PHP教程 > php应用 > 列表

PHP依赖倒置(Dependency Injection)代码实例

发布:smiling 来源: PHP粉丝网  添加日期:2021-04-16 11:12:42 浏览: 评论:0 

这篇文章主要介绍了PHP依赖倒置(Dependency Injection)代码实例本文只提供实现代码,需要的朋友可以参考下

实现类:

  1. <?php 
  2.  
  3. class Container 
  4. { 
  5.     protected $setings = array(); 
  6.  
  7.     public function set($abstract, $concrete = null) 
  8.     { 
  9.         if ($concrete === null) { 
  10.             $concrete = $abstract; 
  11.         } 
  12.  
  13.         $this->setings[$abstract] = $concrete; 
  14.     } 
  15.  
  16.     public function get($abstract, $parameters = array()) 
  17.     { 
  18.         if (!isset($this->setings[$abstract])) { 
  19.             return null; 
  20.         } 
  21.  
  22.         return $this->build($this->setings[$abstract], $parameters); 
  23.     } 
  24.  
  25.     public function build($concrete, $parameters) 
  26.     { 
  27.         if ($concrete instanceof Closure) { 
  28.             return $concrete($this, $parameters); 
  29.         } 
  30.  
  31.         $reflector = new ReflectionClass($concrete); 
  32.  
  33.         if (!$reflector->isInstantiable()) { 
  34.             throw new Exception("Class {$concrete} is not instantiable"); 
  35.         } 
  36.  
  37.         $constructor = $reflector->getConstructor(); 
  38.  
  39.         if (is_null($constructor)) { 
  40.             return $reflector->newInstance(); 
  41.         } 
  42.  
  43.         $parameters = $constructor->getParameters(); 
  44.         $dependencies = $this->getDependencies($parameters); 
  45.  
  46.         return $reflector->newInstanceArgs($dependencies); 
  47.     } 
  48.  
  49.     public function getDependencies($parameters) 
  50.     { 
  51.         $dependencies = array(); 
  52.         foreach ($parameters as $parameter) { 
  53.             $dependency = $parameter->getClass(); 
  54.             if ($dependency === null) { 
  55.                 if ($parameter->isDefaultValueAvailable()) { 
  56.                     $dependencies[] = $parameter->getDefaultValue(); 
  57.                 } else { 
  58.                     throw new Exception("Can not be resolve class dependency {$parameter->name}"); 
  59.                 } //www.phpfensi.com 
  60.             } else { 
  61.                 $dependencies[] = $this->get($dependency->name); 
  62.             } 
  63.         } 
  64.  
  65.         return $dependencies; 
  66.     } 
  67. } 

实现实例:

  1. <?php 
  2.  
  3. require 'container.php'; 
  4.  
  5.  
  6. interface MyInterface{} 
  7. class Foo implements MyInterface{} 
  8. class Bar implements MyInterface{} 
  9. class Baz 
  10. { 
  11.     public function __construct(MyInterface $foo) 
  12.     { 
  13.         $this->foo = $foo; 
  14.     } 
  15. } 
  16.  
  17. $container = new Container(); 
  18. $container->set('Baz', 'Baz'); 
  19. $container->set('MyInterface', 'Foo'); 
  20. $baz = $container->get('Baz'); 
  21. print_r($baz); 
  22. $container->set('MyInterface', 'Bar'); 
  23. $baz = $container->get('Baz'); 
  24. print_r($baz); 

Tags: PHP依赖倒置

分享到: