PHP写的一个轻量级的DI容器类(转)
?理解什么是Di/IoC,依賴注入/控制反轉。兩者說的是一個東西,是當下流行的一種設計模式。大致的意思就是,準備一個盒子(容器),事先將項目中可能用到的類扔進去,在項目中直接從容器中拿,也就是避免了直接在項目中到處new,造成大量耦合。取而代之的是在項目類里面增設 setDi()和getDi()方法,通過Di同一管理類。 當然,以上內容并不是重點,詳細的概念推薦參考這篇文章: http://docs.phalconphp.com/en/latest/reference/di.html 中文版: http://phalcon.5iunix.net/reference/di.html
?
<?php class Di implements \ArrayAccess{private $_bindings = array();//服務列表private $_instances = array();//已經實例化的服務//獲取服務public function get($name,$params=array()){//先從已經實例化的列表中查找if(isset($this->_instances[$name])){return $this->_instances[$name];}//檢測有沒有注冊該服務if(!isset($this->_bindings[$name])){return null;}$concrete = $this->_bindings[$name]['class'];//對象具體注冊內容$obj = null;//匿名函數方式if($concrete instanceof \Closure){$obj = call_user_func_array($concrete,$params);}//字符串方式elseif(is_string($concrete)){if(empty($params)){$obj = new $concrete;}else{//帶參數的類實例化,使用反射$class = new \ReflectionClass($concrete);$obj = $class->newInstanceArgs($params);}}//如果是共享服務,則寫入_instances列表,下次直接取回if($this->_bindings[$name]['shared'] == true && $obj){$this->_instances[$name] = $obj;}return $obj;}//檢測是否已經綁定public function has($name){return isset($this->_bindings[$name]) or isset($this->_instances[$name]);}//卸載服務public function remove($name){unset($this->_bindings[$name],$this->_instances[$name]);}//設置服務public function set($name,$class){$this->_registerService($name, $class);}//設置共享服務public function setShared($name,$class){$this->_registerService($name, $class, true);}//注冊服務private function _registerService($name,$class,$shared=false){$this->remove($name);if(!($class instanceof \Closure) && is_object($class)){$this->_instances[$name] = $class;}else{$this->_bindings[$name] = array("class"=>$class,"shared"=>$shared);}}//ArrayAccess接口,檢測服務是否存在public function offsetExists($offset) {return $this->has($offset);}//ArrayAccess接口,以$di[$name]方式獲取服務public function offsetGet($offset) {return $this->get($offset);}//ArrayAccess接口,以$di[$name]=$value方式注冊服務,非共享public function offsetSet($offset, $value) {return $this->set($offset,$value);}//ArrayAccess接口,以unset($di[$name])方式卸載服務public function offsetUnset($offset) {return $this->remove($offset);} }演示:
<?php header("Content-Type:text/html;charset=utf8"); class A{public $name;public $age;public function __construct($name=""){$this->name = $name;} }include "Di.class.php"; $di = new Di(); //匿名函數方式注冊一個名為a1的服務 $di->setShared('a1',function($name=""){return new A($name); }); //直接以類名方式注冊 $di->set('a2','A'); //直接傳入實例化的對象 $di->set('a3',new A("小唐"));$a1 = $di->get('a1',array("小李")); echo $a1->name."<br/>";//小李 $a1_1 = $di->get('a1',array("小王")); echo $a1->name."<br/>";//小李 echo $a1_1->name."<br/>";//小李$a2 = $di->get('a2',array("小張")); echo $a2->name."<br/>";//小張 $a2_1 = $di->get('a2',array("小徐")); echo $a2->name."<br/>";//小張 echo $a2_1->name."<br/>";//小徐$a3 = $di['a3'];//可以直接通過數組方式獲取服務對象 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');
?
轉載于:https://www.cnblogs.com/hubing/p/5222596.html
總結
以上是生活随笔為你收集整理的PHP写的一个轻量级的DI容器类(转)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 值传递和引用传递的讲解
- 下一篇: php artisan 命令