La clase ReflectionMethod te permite hacer ingenieria inversa de los métodos de la clase.
Para entender los métodos, primero tendrá que crear una instancia de la clase
ReflectionMethod. Puede entonces llamar cualquiera de los métodos anteriores en esta instancia.
Ejemplo 19-36. Usando la clase ReflectionMethod
<?phpclass Counter{ private static $c = 0;
/** * Increment counter * * @final * @static * @access public * @return int */ final public static function increment() { return ++self::$c; } } // Create an instance of the Reflection_Method class$method = new ReflectionMethod('Counter', 'increment'); // Print out basic informationprintf( "===> The %s%s%s%s%s%s%s method '%s' (which is %s)\n" . " declared in %s\n" . " lines %d to %d\n" . " having the modifiers %d[%s]\n", $method->isInternal() ? 'internal' : 'user-defined', $method->isAbstract() ? ' abstract' : '', $method->isFinal() ? ' final' : '', $method->isPublic() ? ' public' : '', $method->isPrivate() ? ' private' : '', $method->isProtected() ? ' protected' : '', $method->isStatic() ? ' static' : '', $method->getName(), $method->isConstructor() ? 'the constructor' : 'a regular method', $method->getFileName(), $method->getStartLine(), $method->getEndline(), $method->getModifiers(), implode(' ', Reflection::getModifierNames($method->getModifiers())) ); // Print documentation commentprintf("---> Documentation:\n %s\n", var_export($method->getDocComment(), 1)); // Print static variables if existantif ($statics= $method->getStaticVariables()) { printf("---> Static variables: %s\n", var_export($statics, 1)); } // Invoke the methodprintf("---> Invokation results in: ");var_dump($method->invoke(NULL));?> |
|
Nota: Tratar de invocar métodos private, protected o abstract resultará en una excepción siendo arrojada del método invoke().
Nota: Para métodos static como se vió anteriormente, se debe a invoke() se debe pasar NULL como primer argumento. Para métodos no estáticos, se pasa una instancia de la clase.
No response to “ReflectionMethod”
Publicar un comentario