本文实例讲述了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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
<?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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
/**
* 执行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(&$controller, $action)); // 其实吧,用这个函数足够啦!!!
try {
$methodInfo = new \\ReflectionMethod($namespaceClassName, $action);
if ($methodInfo->isPublic() && !$methodInfo->isStatic()) {
$methodInfo->invoke($controller);
} else { // 操作方法不是public类型,抛出异常
throw new \\ReflectionException();
}
} catch (\\ReflectionException $e) {
// 方法调用发生异常后,引导到__call方法处理
$methodInfo = new \\ReflectionMethod($namespaceClassName, '__call');
$methodInfo->invokeArgs($controller, array($action, ''));
}
return;
}
|
相关文章
猜你喜欢
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-27 20
-
2025-05-29 62
-
2025-05-27 97
-
2025-05-27 63
-
2025-05-25 61
热门评论

