ReflectionFunction

La clase ReflectionFunction te permite funciones de ingeniería inversa.



<?phpclass ReflectionFunction implements Reflector{
    
final private __clone()
    
public object __construct(string name)
    
public string __toString()
    
public static string export()
    
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 mixed invoke(mixedargs)
    
public mixed invokeArgs(array args)
    
public bool returnsReference()
    
public ReflectionParameter[] getParameters()
    
public int getNumberOfParameters()
    
public int getNumberOfRequiredParameters()
}
?>

Nota: invokeArgs() fue agregado en PHP 5.1.0.
Para entender directamente una función, primero tiene que crear una isntancia de la clase ReflectionFunction. Hasta entonces puede llamar cualquier de los métodos anteriores en esta instancia.
Ejemplo 19-33. Usando la clase ReflectionFunction
<?php/**
* A simple counter
*
* @return    int
*/
function counter()
{
    static 
$c 0;
    return 
$c++;
}
// Create an instance of the Reflection_Function class$func = new ReflectionFunction('counter');
// Print out basic informationprintf(
    
"===> The %s function '%s'\n".
    
"     declared in %s\n".
    
"     lines %d to %d\n",
    
$func->isInternal() ? 'internal' 'user-defined',
    
$func->getName(),
    
$func->getFileName(),
    
$func->getStartLine(),
    
$func->getEndline()
);
// Print documentation commentprintf("---> Documentation:\n %s\n"var_export($func->getDocComment(), 1));
// Print static variables if existantif ($statics $func->getStaticVariables())
{
    
printf("---> Static variables: %s\n"var_export($statics1));
}
// Invoke the functionprintf("---> Invokation results in: ");var_dump($func->invoke());

// you may prefer to use the export() methodecho "\nReflectionFunction::export() results:\n";
echo 
ReflectionFunction::export('counter');?>
Nota: El método invoke() acepta un número de variable de argumentos los cuales son pasados a la función tal y como se hace en call_user_func().

No response to “ReflectionFunction”