本文共 1277 字,大约阅读时间需要 4 分钟。
什么是php反射类,顾名思义,可以理解为一个类的映射。 举个例子:Class [ class A ] { @@ F:\phpweb\myPHP\test.php 23-30 - Constants [0] { } - Static properties [0] { } - Static methods [0] { } - Properties [0] { } - Methods [1] { Method [ public method __construct ] { @@ F:\phpweb\myPHP\test.php 26 - 29 } } }
举例
try{//如果存在控制器名字的类if(class_exists($this->getController())) {//利用反射api构造一个控制器类对应的反射类$rc = new ReflectionClass($this->getController());//如果该类实现 了IController接口if($rc->implementsInterface('IController')) {//该类拥有解析后的action字符串所指向的方法名if($rc->hasMethod($this->getAction())) {//构造一个控制器类的实例$controller = $rc->newInstance();//获取该类$action参数所指向的方法对象$method = $rc->getMethod($this->getAction());//反射类方法对象的调用方式:$method->invoke($controller);} else {//以下为可能抛出异常throw new Exception("Action");}} else {throw new Exception("Interface");}} else {throw new Exception("Controller");} }catch(exception $e) { echo $e; }
转载地址:http://rrwvf.baihongyu.com/