php 反射 thinkphp,php 反射实例应用-thinkphp 控制器调度

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!';

标签:PHP

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值