获取一个php类的方法,多会用到get_class_methods(),可手册中并没说明此函数返回的只是public类型的方法。
如果想要获取到包括私有和保护的所有方法,那需要用到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);
结果:
array(3) {
[0]=>
array(3) {
["type"]=>
string(7) "private"
["name"]=>
string(7) "prifunc"
["class"]=>
string(3) "foo"
}
[1]=>
array(3) {
["type"]=>
string(9) "protected"
["name"]=>
string(7) "profunc"
["class"]=>
string(3) "foo"
}
[2]=>
array(3) {
["type"]=>
string(6) "public"
["name"]=>
string(7) "pubfunc"
["class"]=>
string(3) "foo"
}
}