ReflectionMethod

La clase ReflectionMethod te permite hacer ingenieria inversa de los métodos de la clase.



<?phpclass ReflectionMethod extends ReflectionFunction{
    
public __construct(mixed class, string name)
    
public string __toString()
    
public static string export()
    
public mixed invoke(stdclass objectmixedargs)
    
public moxed invokeArgs(stdclass object, array args)
    
public bool isFinal()
    
public bool isAbstract()
    
public bool isPublic()
    
public bool isPrivate()
    
public bool isProtected()
    
public bool isStatic()
    
public bool isConstructor()
    
public bool isDestructor()
    
public int getModifiers()
    
public ReflectionClass getDeclaringClass()

    
// Inherited from ReflectionFunction
    
final private __clone()
    
public string getName()
    
public bool isInternal()
    
public bool isUserDefined()
    
public string getFileName()
    
public int getStartLine()
    
public int getEndLine()
    
public string getDocComment()
    
public array getStaticVariables()
    
public bool returnsReference()
    
public ReflectionParameter[] getParameters()
    
public int getNumberOfParameters()
    
public int getNumberOfRequiredParameters()
}
?>

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($statics1));
}
// 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”