想要获取一个PHP类的方法,大家可能很自然的想到了get_class_methods(),可手册中并没说明此函数返回的只是public类型的方法。
如果想要获取到包括私有和保护的所有方法,那需要用到PHP中反射类,还是通过例子来说明吧。
<?php class Foo { private function priFunc(){} protected function proFunc(){} public function pubFunc(){} } function get_class_all_methods($class){ $r = new ReflectionClass($class); foreach($r->getMethods() as $key=>$methodObj){ if($methodObj->isPrivate()) $methods[$key]['type'] = 'private'; elseif($methodObj->isProtected()) $methods[$key]['type'] = 'protected'; else $methods[$key]['type'] = 'public'; $methods[$key]['name'] = $methodObj->name; $methods[$key]['class'] = $methodObj->class; } return $methods; } $methods = get_class_all_methods('Foo'); var_dump($methods);
浙公网安备 33010602011771号