反射类
当你需要确定一个API的时候,却没有相关的文档,可以尝试使用PHP的ReflectorClass来得到相关信息
1.获取类方法的参数名
public function getparams() { include APPPATH . 'controllers\obj.php'; $r = new ReflectionClass("Obj"); foreach ($r->getMethod('getinfo')->getParameters() as $param) { var_dump((string) $param); } }
结果如图
2.在工厂模式中实例化多个类调调用方法
public function factory() { $type = $this->input->get('type'); switch ((int) $type) { case 1: include APPPATH . 'controllers\obj.php'; $r = new ReflectionClass("Obj"); $instance = $r->newInstance(); return $instance->payWeixin(); break; case 2: include APPPATH . 'controllers\Html.php'; $r = new ReflectionClass("Html"); $instance = $r->newInstance(); return $instance->payAlipay(); break; } }