1.如何通过地址栏参数来得到模块名称和控制器名称(即使在有路由和开了重写模块的情况下)
2.tp是如何实现前置,后置方法功能模块,和如何执行带参数的方法?
php系统自带的 ReflectionClass,ReflectionMethod 类,可以反射用户自定义类的中属性,方法的权限和参数等信息,通过这些信息可以准确的控制方法的执行
ReflectionClass主要用的方法:
hasMethod(string) 是否存在某个方法
getMethod(string) 获取方法
ReflectionMethod 主要方法:
getNumberOfParameters() 获取参数个数
getParamters() 获取参数信息
3.代码演示
复制代码 代码如下:
<?php
class IndexAction{
public function index(){
echo 'index'."\r\n";
}
public function test($year=2012,$month=2,$day=21){
echo $year.'--------'.$month.'-----------'.$day."\r\n";
}
public function _before_index(){
echo __FUNCTION__."\r\n";
}
public function _after_index(){
echo __FUNCTION__."\r\n";
}
}
//执行index方法
$method = new ReflectionMethod('IndexAction','index');
//进行权限判断
if($method->isPublic()){
$class = new ReflectionClass('IndexAction');
//执行前置方法
if($class->hasMethod('_before_index')){
$beforeMethod = $class->getMethod('_before_index');
if($beforeMethod->isPublic()){
$beforeMethod->invoke(new IndexAction);
}
}
$method->invoke(new IndexAction);
//执行后置方法
if($class->hasMethod('_after_index')){
$beforeMethod = $class->getMethod('_after_index');
if($beforeMethod->isPublic()){
$beforeMethod->invoke(new IndexAction);
}
}
}
//执行带参数的方法
$method = new ReflectionMethod('IndexAction','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(new IndexAction,$args);
else
echo 'parameters is not match!';
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 华少翌.2005-华少翌【中唱艺能】【WAV+CUE】
- 群星《十二女声Ⅱ》[WAV/CUE/分轨][527MB]
- 大自然音乐系列《漫步在山裡》[WAV/CUE/分轨][502.8MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]
- 史依弘《史依弘交响乐京剧演唱专辑HQ》头版限量编号[WAV+CUE]
- 曼丽2020《女人三十风继续吹》HQCD[WAV+CUE]
- 曼丽2021《女人三十2雾之恋》HQCD[WAV+CUE]
- 隔壁老樊.2019-我曾【好听音乐】【FLAC分轨】
- 高林生.1993-牵挂你的人是我【白天鹅】【WAV+CUE】
- 洪卓立.2012-找个懂我的女孩(国专)【英皇娱乐】【WAV+CUE】
- 曼丽2022《女人三十3晚秋》HQCD[WAV+CUE]
- 曼丽2018《女人三十4冬天》HQCD[WAV+CUE]
- 柏菲-《好歌30年壹》限量开盘母带ORMCD[低速原抓WAV+CUE]
- 大自然音乐系列《水舞》[WAV/CUE/分轨][424.3MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]