解析thinkPHP基于反射實現鉤子的方法

下面由thinkphp框架教程欄目給大家解析thinkphp基于反射實現鉤子的方法,希望對需要的朋友有所幫助!

thinkphp框架的控制器模塊是如何實現 前控制器、后控制器,及如何執行帶參數的方法?

PHP系統自帶的 ReflectionClass、ReflectionMethod 類,可以反射用戶自定義類的中屬性,方法的權限和參數等信息,通過這些信息可以準確的控制方法的執行。

ReflectionClass:

主要用的方法:

立即學習PHP免費學習筆記(深入)”;

hasMethod(String)? 是否存在某個方法
getMethod(string)? 獲取方法

ReflectionMethod:

主要方法:

ispublic()??? 是否為 public 方法
getNumberOfParameters()? 獲取參數個數
getParamters()? 獲取參數信息
invoke( Object $object [, mixed $parameter [, mixed $… ]] ) 執行方法
invokeArgs(object obj, Array args)??帶參數執行方法

實例演示

<?php class BlogAction {   public function detail() {     echo &#39;detail&#39; . "  ";   }   public function test($year = 2014, $month = 4, $day = 21) {     echo $year . &#39;--&#39; . $month . &#39;--&#39; . $day . "  ";   }   public function _before_detail() {     echo __FUNCTION__ . "  ";   }   public function _after_detail() {     echo __FUNCTION__ . "  ";   } } // 執行detail方法 $method = new ReflectionMethod(&#39;BlogAction&#39;, &#39;detail&#39;); $instance = new BlogAction(); // 進行權限判斷 if ($method->isPublic())?{ ??$class?=?new?ReflectionClass('BlogAction'); ??//?執行前置方法 ??if?($class-&gt;hasMethod('_before_detail'))?{ ????$beforeMethod?=?$class-&gt;getMethod('_before_detail'); ????if?($beforeMethod-&gt;isPublic())?{ ??????$beforeMethod-&gt;invoke($instance); ????} ??} ??$method-&gt;invoke(new?BlogAction); ??//?執行后置方法 ??if?($class-&gt;hasMethod('_after_detail'))?{ ????$beforeMethod?=?$class-&gt;getMethod('_after_detail'); ????if?($beforeMethod-&gt;isPublic())?{ ??????$beforeMethod-&gt;invoke($instance); ????} ??} } //?執行帶參數的方法 $method?=?new?ReflectionMethod('BlogAction',?'test'); $params?=?$method-&gt;getParameters(); foreach?($params?as?$param)?{ ??$paramName?=?$param-&gt;getName(); ??if?(isset($_REQUEST[$paramName]))?{ ????$args[]?=?$_REQUEST[$paramName]; ??}?elseif?($param-&gt;isDefaultValueAvailable())?{ ????$args[]?=?$param-&gt;getDefaultValue(); ??} } if?(count($args)?==?$method-&gt;getNumberOfParameters())?{ ??$method-&gt;invokeArgs($instance,?$args); }?else?{ ??echo?'parameters?is?wrong!'; }

另一段代碼參考

/** ?*?執行App控制器 ?*/ public?function?execApp()?{ ??//?創建action控制器實例 ??$className?=?MODULE_NAME?.?'Controller'; ??$namespaceClassName?=?'apps'?.?APP_NAME?.?'controller'?.?$className; ??load_class($namespaceClassName,?false); ??if?(!class_exists($namespaceClassName))?{ ????throw?new?Exception('Oops!?Module?not?found?:?'?.?$namespaceClassName); ??} ??$controller?=?new?$namespaceClassName(); ??//?獲取當前操作名 ??$action?=?ACTION_NAME; ??//?執行當前操作 ??//call_user_func(array(&amp;$controller,?$action));?//?其實吧,用這個函數足夠啦!!! ??try?{ ????$methodInfo?=?new?ReflectionMethod($namespaceClassName,?$action); ????if?($methodInfo-&gt;isPublic()?&amp;&amp;?!$methodInfo-&gt;isStatic())?{ ??????$methodInfo-&gt;invoke($controller); ????}?else?{?//?操作方法不是public類型,拋出異常 ??????throw?new?ReflectionException(); ????} ??}?catch?(ReflectionException?$e)?{ ????//?方法調用發生異常后,引導到__call方法處理 ????$methodInfo?=?new?ReflectionMethod($namespaceClassName,?'__call'); ????$methodInfo-&gt;invokeArgs($controller,?array($action,?'')); ??} ??return; }

相關推薦:最新的10個thinkphp視頻教程

? 版權聲明
THE END
喜歡就支持一下吧
點贊14 分享