PHP5添加了一项新的功能:Reflection。这个功能使得phper可以reverse-engineer class, interface,function,method and extension。通过PHP代码,就可以得到某object的所有信息,并且可以和它交互
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);