解析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 分享