Rumah > php教程 > php手册 > teks badan

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

WBOY
Lepaskan: 2016-06-06 19:33:02
asal
828 orang telah melayarinya

无详细内容 无 ?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!';

Salin selepas log masuk
php 反射实例应用-thinkphp 控制器调度
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!