One of the nicer, and largely unheralded I think, features of PHP 5 is its comprehensive reflection API. Arguably one of the reasons it is largely unheralded is because its documentation is a bit average. One great little tidbit, and the motivation for the blog post, is the ReflectionClass->getMethods($filter=null) method takes an optional parameter 'filter'. In the online documentation there is scant mention of what values this $filter parameter can take. Luckily in the comments Will Mason chimed in with paydirt:
If you are looking for the long $filters for ReflectionClass::getMethods(), here they are. They took me a long time to find. Found nothing in the docs, nor google. But of course, Reflection itself was the final solution, in the form of ReflectionExtension::export("Reflection").
// The missing long $filter values!!! ReflectionMethod::IS_STATIC; ReflectionMethod::IS_PUBLIC; ReflectionMethod::IS_PROTECTED; ReflectionMethod::IS_PRIVATE; ReflectionMethod::IS_ABSTRACT; ReflectionMethod::IS_FINAL; // Use them like this $r = new ReflectionClass("MyClass"); // Print all public methods foreach ($r->getMethods(ReflectionMethod::IS_PUBLIC) as $m) { echo $m->__toString(); }
Another example, this time one of my own, is one that I found myself writing while working with the Zend Framework's Zend_Controller implementation:
/** * @param String $controller_class * @return ArrayObject */ private function getActionList($controller_class) { $reflection_class = new ReflectionClass($controller_class); $methods = $reflection_class->getMethods(ReflectionProperty::IS_PUBLIC); return new ArrayObject($methods); }
Like many platforms, in PHP it seems documentation is no replacement for digging around the source itself.