首頁 > php教程 > php手册 > 主體

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

WBOY
發布: 2016-06-06 19:33:02
原創
827 人瀏覽過

无详细内容 无 ?phpclass 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__."\

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

登入後複製
php 反射实例应用-thinkphp 控制器调度
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!