php写一个轻量级的容器

原文地址: https://my.oschina.net/cxz001/blog/227482

参考地址:https://segmentfault.com/a/1190000002424023

参考地址:http://www.digpage.com/di.html


摘要: 理解什么是Di/IoC,依赖注入/控制反转。两者说的是一个东西,是当下流行的一种设计模式。大致的意思就是,准备一个盒子(容器),事先将项目中可能用到的类扔进去,在项目中直接从容器中拿,也就是避免了直接在项目中到处new,造成大量耦合。取而代之的是在项目类里面增设 setDi()和getDi()方法,通过Di同一管理类。 当然,以上内容并不是重点,详细的概念推荐参考这篇文章: http://docs.phalconphp.com/en/latest/reference/di.html 中文版: http://phalcon.5iunix.net/reference/di.html

直接上代码吧.

Di容器类:

[php]  view plain  copy
  1. class Di implements \ArrayAccess{  
  2.     private $_bindings = array();//服务列表  
  3.     private $_instances = array();//已经实例化的服务  
  4.       
  5.     //获取服务  
  6.     public function get($name,$params=array()){  
  7.         //先从已经实例化的列表中查找  
  8.         if(isset($this->_instances[$name])){  
  9.             return $this->_instances[$name];  
  10.         }  
  11.           
  12.         //检测有没有注册该服务  
  13.         if(!isset($this->_bindings[$name])){  
  14.             return null;  
  15.         }  
  16.           
  17.         $concrete = $this->_bindings[$name]['class'];//对象具体注册内容  
  18.           
  19.         $obj = null;  
  20.         //匿名函数方式  
  21.         if($concrete instanceof \Closure){  
  22.             $obj = call_user_func_array($concrete,$params);  
  23.         }elseif(is_string($concrete)){//字符串方式  
  24.             if(empty($params)){  
  25.                 $obj = new $concrete;  
  26.             }else{  
  27.                 //带参数的类实例化,使用反射  
  28.                 $class = new \ReflectionClass($concrete);  
  29.                 $obj = $class->newInstanceArgs($params);  
  30.             }  
  31.         }  
  32.           
  33.         //如果是共享服务,则写入_instances列表,下次直接取回  
  34.         if($this->_bindings[$name]['shared'] == true && $obj){  
  35.             $this->_instances[$name] = $obj;  
  36.         }  
  37.           
  38.         return $obj;  
  39.     }  
  40.       
  41.     //检测是否已经绑定  
  42.     public function has($name){  
  43.         return isset($this->_bindings[$name]) or isset($this->_instances[$name]);  
  44.     }  
  45.       
  46.     //卸载服务  
  47.     public function remove($name){  
  48.         unset($this->_bindings[$name],$this->_instances[$name]);  
  49.     }  
  50.       
  51.     //设置服务  
  52.     public function set($name,$class){  
  53.         $this->_registerService($name$class);  
  54.     }  
  55.       
  56.     //设置共享服务  
  57.     public function setShared($name,$class){  
  58.         $this->_registerService($name$class, true);  
  59.     }  
  60.       
  61.     //注册服务  
  62.     private function _registerService($name,$class,$shared=false){  
  63.         $this->remove($name);  
  64.         if(!($class instanceof \Closure) && is_object($class)){  
  65.             $this->_instances[$name] = $class;  
  66.         }else{  
  67.             $this->_bindings[$name] = array("class"=>$class,"shared"=>$shared);  
  68.         }  
  69.     }  
  70.       
  71.     //ArrayAccess接口,检测服务是否存在  
  72.     public function offsetExists($offset) {  
  73.         return $this->has($offset);  
  74.     }  
  75.       
  76.     //ArrayAccess接口,以$di[$name]方式获取服务  
  77.     public function offsetGet($offset) {  
  78.         return $this->get($offset);  
  79.     }  
  80.       
  81.     //ArrayAccess接口,以$di[$name]=$value方式注册服务,非共享  
  82.     public function offsetSet($offset$value) {  
  83.         return $this->set($offset,$value);  
  84.     }  
  85.       
  86.     //ArrayAccess接口,以unset($di[$name])方式卸载服务  
  87.     public function offsetUnset($offset) {  
  88.         return $this->remove($offset);  
  89.     }  
  90. }  


演示:


[php]  view plain  copy
  1. <?php  
  2. header("Content-Type:text/html;charset=utf8");  
  3. class A{  
  4.     public $name;  
  5.     public $age;  
  6.     public function __construct($name=""){  
  7.         $this->name = $name;  
  8.     }  
  9. }  
  10.   
  11. include "Di.class.php";  
  12. $di = new Di();  
  13. //匿名函数方式注册一个名为a1的服务  
  14. $di->setShared('a1',function($name=""){  
  15.     return new A($name);  
  16. });  
  17. //直接以类名方式注册  
  18. $di->set('a2','A');  
  19. //直接传入实例化的对象  
  20. $di->set('a3',new A("小唐"));  
  21.   
  22. $a1 = $di->get('a1',array("小李"));  
  23. echo $a1->name."<br/>";//小李  
  24. $a1_1 = $di->get('a1',array("小王"));  
  25. echo $a1->name."<br/>";//小李  
  26. echo $a1_1->name."<br/>";//小李  
  27.   
  28. $a2 = $di->get('a2',array("小张"));  
  29. echo $a2->name."<br/>";//小张  
  30. $a2_1 = $di->get('a2',array("小徐"));  
  31. echo $a2->name."<br/>";//小张  
  32. echo $a2_1->name."<br/>";//小徐  
  33.   
  34. $a3 = $di['a3'];//可以直接通过数组方式获取服务对象  
  35. echo $a3->name."<br/>";//小唐  


通过set和setShared方式注册服务,支持 匿名函数,类名字符串,已经实例化的对象.

两者的区别是:set方式注册的,每次获取的时候都会重新实例化setShared方式的,则只实例化一次,也就是所谓的单例模式
当然,对于直接注册已经实例化的对象,如上代码中的a3服务,set和setShared效果是一样的。

通过$di->get()获取服务,可接受两个参数,第一个参数是服务名,比如a1,a2,a3是必须的,第二个参数是一个数组,第二个参数会被当作匿名函数中的参数或者类构造函数里的参数传进去,参考call_user_func_array()。

删除服务则可以通过

unset($di['a1']);

or

$di->remove('a1');


判断是否包含一个服务可以通过

isset($di['a1']);

or

$di->has('a1');

就这么多了。


猜你喜欢

转载自blog.csdn.net/qq_32439101/article/details/79626974