PHP反射(ReflectionClass、ReflectionMethod)在ThinkPHP框架的控制器调度模块中的应用

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

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

ReflectionClass:  [PHP手册]详情

主要用的方法:

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

getMethod(string)  获取方法

ReflectionMethod:  [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!';
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
了解了您的需求,我可以为您提供一个思路,假设您使用的是 ThinkPHP 6 框架,您可以通过以下步骤实现: 1.使用反射机制获取已有控制器的类名和方法名。 ```php $controllerName = 'app\controller\YourController'; // 已有控制器的类名 $methodName = 'yourMethod'; // 已有控制器的方法名 $reflectionClass = new \ReflectionClass($controllerName); // 实例化反射类 $reflectionMethod = $reflectionClass->getMethod($methodName); // 获取反射方法 ``` 2.通过反射机制获取已有控制器方法的参数列表。 ```php $params = []; foreach($reflectionMethod->getParameters() as $param) { if ($param->isDefaultValueAvailable()) { $params[] = $param->getDefaultValue(); } else { $params[] = null; } } ``` 3.通过反射机制获取已有控制器方法的注释,包括路由和参数注释。 ```php $docComment = $reflectionMethod->getDocComment(); preg_match('/@route\W+(.*)/', $docComment, $routeMatch); $route = isset($routeMatch[1]) ? $routeMatch[1] : ''; preg_match_all('/@param\W+(\w+)\W+(\$[a-zA-Z0-9_]+)/', $docComment, $paramMatches); $paramsDoc = []; foreach ($paramMatches[2] as $index => $paramName) { $paramsDoc[$paramName] = $paramMatches[1][$index]; } ``` 4.使用获取到的信息,动态生成新的控制器方法。 ```php $newMethodName = 'newMethod'; // 新的控制器方法名 $newControllerContent = <<<CONTROLLER <?php namespace app\controller; use think\Controller; class NewController extends Controller { /** * $docComment */ public function $newMethodName($paramsDoc) { // 在此处编写新的控制器方法逻辑 } } CONTROLLER; file_put_contents('app/controller/NewController.php', $newControllerContent); // 将新的控制器文件写入磁盘 ``` 需要注意的是,以上代码仅为思路示例,具体实现可能需要根据您的需求进行调整。同时,自动生成的代码需要您自己进行调试和测试,以确保代码的正确性和稳定性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值