thinkPHP基于反射实现钩子的方法分析
本文实例讲述了thinkPHP基于反射实现钩子的方法。分享给大家供大家参考,具体如下:
ThinkPHP框架的控制器模块是如何实现前控制器、后控制器,及如何执行带参数的方法?
PHP系统自带的ReflectionClass、ReflectionMethod类,可以反射用户自定义类的中属性,方法的权限和参数等信息,通过这些信息可以准确的控制方法的执行。
ReflectionClass:
主要用的方法:
hasMethod(string) 是否存在某个方法
getMethod(string) 获取方法
ReflectionMethod:
主要方法:
isPublic() 是否为public方法
getNumberOfParameters() 获取参数个数
getParamters() 获取参数信息
invoke(object$object[,mixed$parameter[,mixed$...]])执行方法
invokeArgs(objectobj,arrayargs) 带参数执行方法
实例演示
isPublic()){
$class=newReflectionClass('BlogAction');
//执行前置方法
if($class->hasMethod('_before_detail')){
$beforeMethod=$class->getMethod('_before_detail');
if($beforeMethod->isPublic()){
$beforeMethod->invoke($instance);
}
}
$method->invoke(newBlogAction);
//执行后置方法
if($class->hasMethod('_after_detail')){
$beforeMethod=$class->getMethod('_after_detail');
if($beforeMethod->isPublic()){
$beforeMethod->invoke($instance);
}
}
}
//执行带参数的方法
$method=newReflectionMethod('BlogAction','test');
$params=$method->getParameters();
foreach($paramsas$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'parametersiswrong!';
}
另一段代码参考
/**
*执行App控制器
*/
publicfunctionexecApp(){
//创建action控制器实例
$className=MODULE_NAME.'Controller';
$namespaceClassName='\\apps\\'.APP_NAME.'\\controller\\'.$className;
load_class($namespaceClassName,false);
if(!class_exists($namespaceClassName)){
thrownew\Exception('Oops!Modulenotfound:'.$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类型,抛出异常
thrownew\ReflectionException();
}
}catch(\ReflectionException$e){
//方法调用发生异常后,引导到__call方法处理
$methodInfo=new\ReflectionMethod($namespaceClassName,'__call');
$methodInfo->invokeArgs($controller,array($action,''));
}
return;
}
更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《ZendFrameWork框架入门教程》及《PHP模板技术总结》。
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。