thinkphp框架的控制器模块是如何实现 前控制器、后控制器,及如何执行带参数的方法?
PHP系统自带的 ReflectionClass、ReflectionMethod 类,可以反射用户自定义类的中属性,方法的权限和参数等信息,通过这些信息可以准确的控制方法的执行。
ReflectionClass: [PHP手册]详情
主要用的方法:hasMethod(string) 是否存在某个方法getMethod(string) 获取方法
ReflectionMethod: [PHP手册]详情
主要方法:
立即学习“PHP免费学习笔记(深入)”;
isPublic() 是否为 public 方法
getNumberOfParameters() 获取参数个数
getParamters() 获取参数信息
invoke( object $object [, mixed $parameter [, mixed $... ]] ) 执行方法
invokeArgs(object obj, array args) 带参数执行方法
实例演示:
<?php class BlogAction { public function detail() { echo 'detail' . "\r\n"; } public function test($year = 2014, $month = 4, $day = 21) { echo $year . '--' . $month . '--' . $day . "\r\n"; } public function _before_detail() { echo __FUNCTION__ . "\r\n"; } public function _after_detail() { echo __FUNCTION__ . "\r\n"; } } // 执行detail方法 $method = new ReflectionMethod('BlogAction', 'detail'); $instance = new BlogAction(); // 进行权限判断 if ($method->isPublic()) { $class = new ReflectionClass('BlogAction'); // 执行前置方法 if ($class->hasMethod('_before_detail')) { $beforeMethod = $class->getMethod('_before_detail'); if ($beforeMethod->isPublic()) { $beforeMethod->invoke($instance); } } $method->invoke(new BlogAction); // 执行后置方法 if ($class->hasMethod('_after_detail')) { $beforeMethod = $class->getMethod('_after_detail'); if ($beforeMethod->isPublic()) { $beforeMethod->invoke($instance); } } } // 执行带参数的方法 $method = new ReflectionMethod('BlogAction', 'test'); $params = $method->getParameters(); foreach ($params as $param) { $paramName = $param->getName(); if (isset($_REQUEST[$paramName])) { $args[] = $_REQUEST[$paramName]; } elseif ($param->isDefaultValueAvailable()) { $args[] = $param->getDefaultValue(); } } if (count($args) == $method->getNumberOfParameters()) { $method->invokeArgs($instance, $args); } else { echo 'parameters is wrong!'; }
<?php class BlogAction { public function detail() { echo 'detail' . "\r\n"; } public function test($year = 2014, $month = 4, $day = 21) { echo $year . '--' . $month . '--' . $day . "\r\n"; } public function _before_detail() { echo __FUNCTION__ . "\r\n"; } public function _after_detail() { echo __FUNCTION__ . "\r\n"; } } // 执行detail方法 $method = new ReflectionMethod('BlogAction', 'detail'); $instance = new BlogAction(); // 进行权限判断 if ($method->isPublic()) { $class = new ReflectionClass('BlogAction'); // 执行前置方法 if ($class->hasMethod('_before_detail')) { $beforeMethod = $class->getMethod('_before_detail'); if ($beforeMethod->isPublic()) { $beforeMethod->invoke($instance); } } $method->invoke(new BlogAction); // 执行后置方法 if ($class->hasMethod('_after_detail')) { $beforeMethod = $class->getMethod('_after_detail'); if ($beforeMethod->isPublic()) { $beforeMethod->invoke($instance); } } } // 执行带参数的方法 $method = new ReflectionMethod('BlogAction', 'test'); $params = $method->getParameters(); foreach ($params as $param) { $paramName = $param->getName(); if (isset($_REQUEST[$paramName])) { $args[] = $_REQUEST[$paramName]; } elseif ($param->isDefaultValueAvailable()) { $args[] = $param->getDefaultValue(); } } if (count($args) == $method->getNumberOfParameters()) { $method->invokeArgs($instance, $args); } else { echo 'parameters is wrong!'; }
以上就介绍了PHP反射ReflectionClass、ReflectionMethod在ThinkPHP框架的控制器调度模块中的应用,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号