解析thinkPHP基于反射实现钩子的方法

 4747

下面给大家解析thinkPHP基于反射实现钩子的方法,希望对需要的朋友有所帮助!


ThinkPHP框架的控制器模块是如何实现 前控制器、后控制器,及如何执行带参数的方法?

PHP系统自带的 ReflectionClass、ReflectionMethod 类,可以反射用户自定义类的中属性,方法的权限和参数等信息,通过这些信息可以准确的控制方法的执行。


ReflectionClass:

主要用的方法:

hasMethod(string) 是否存在某个方法

getMethod(string) 获取方法


ReflectionMethod:

主要方法:

isPublic() 是否为 public 方法

getNumberOfParameters() 获取参数个数

getParamters() 获取参数信息

invoke( object $object [, mixed $parameter [, mixed $... ]] ) 执行方法

invokeArgs(object obj, array args) 带参数执行方法


实例演示

  1. <?php
  2. class BlogAction {
  3.     public function detail() {
  4.         echo 'detail' . "\r\n";
  5.     }
  6.     public function test($year = 2014, $month = 4, $day = 21) {
  7.         echo $year . '--' . $month . '--' . $day . "\r\n";
  8.     }
  9.     public function _before_detail() {
  10.         echo __FUNCTION__ . "\r\n";
  11.     }
  12.     public function _after_detail() {
  13.         echo __FUNCTION__ . "\r\n";
  14.     }
  15. }
  16. // 执行detail方法
  17. $method = new ReflectionMethod('BlogAction', 'detail');
  18. $instance = new BlogAction();
  19. // 进行权限判断
  20. if ($method->isPublic()) {
  21.     $class = new ReflectionClass('BlogAction');
  22.     // 执行前置方法
  23.     if ($class->hasMethod('_before_detail')) {
  24.         $beforeMethod = $class->getMethod('_before_detail');
  25.         if ($beforeMethod->isPublic()) {
  26.             $beforeMethod->invoke($instance);
  27.         }
  28.     }
  29.     $method->invoke(new BlogAction);
  30.     // 执行后置方法
  31.     if ($class->hasMethod('_after_detail')) {
  32.         $beforeMethod = $class->getMethod('_after_detail');
  33.         if ($beforeMethod->isPublic()) {
  34.             $beforeMethod->invoke($instance);
  35.         }
  36.     }
  37. }
  38. // 执行带参数的方法
  39. $method = new ReflectionMethod('BlogAction', 'test');
  40. $params = $method->getParameters();
  41. foreach ($params as $param) {
  42.     $paramName = $param->getName();
  43.     if (isset($_REQUEST[$paramName])) {
  44.         $args[] = $_REQUEST[$paramName];
  45.     } elseif ($param->isDefaultValueAvailable()) {
  46.         $args[] = $param->getDefaultValue();
  47.     }
  48. }
  49. if (count($args) == $method->getNumberOfParameters()) {
  50.     $method->invokeArgs($instance, $args);
  51. } else {
  52.     echo 'parameters is wrong!';
  53. }

另一段代码参考

  1. /**
  2.  * 执行App控制器
  3.  */
  4. public function execApp() {
  5.     // 创建action控制器实例
  6.     $className = MODULE_NAME . 'Controller';
  7.     $namespaceClassName = '\\apps\\' . APP_NAME . '\\controller\\' . $className;
  8.     load_class($namespaceClassName, false);
  9.     if (!class_exists($namespaceClassName)) {
  10.         throw new \Exception('Oops! Module not found : ' . $namespaceClassName);
  11.     }
  12.     $controller = new $namespaceClassName();
  13.     // 获取当前操作名
  14.     $action = ACTION_NAME;
  15.     // 执行当前操作
  16.     //call_user_func(array(&$controller, $action)); // 其实吧,用这个函数足够啦!!!
  17.     try {
  18.         $methodInfo = new \ReflectionMethod($namespaceClassName, $action);
  19.         if ($methodInfo->isPublic() && !$methodInfo->isStatic()) {
  20.             $methodInfo->invoke($controller);
  21.         } else { // 操作方法不是public类型,抛出异常
  22.             throw new \ReflectionException();
  23.         }
  24.     } catch (\ReflectionException $e) {
  25.         // 方法调用发生异常后,引导到__call方法处理
  26.         $methodInfo = new \ReflectionMethod($namespaceClassName, '__call');
  27.         $methodInfo->invokeArgs($controller, array($action, ''));
  28.     }
  29.     return;
  30. }


TAG标签:
本文网址:https://www.zztuku.com/index.php/detail-8901.html
站长图库 - 解析thinkPHP基于反射实现钩子的方法
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐