ReflectionFunction::__construct

(PHP 5, PHP 7, PHP 8)

ReflectionFunction::__constructConstruye un nuevo objeto ReflectionFunction

Descripción

public function ReflectionFunction::__construct(Closure|string $function)

Construye un nuevo objeto ReflectionFunction.

Parámetros

function

El nombre de la función a reflejar o una fermeture.

Errores/Excepciones

Se lanza una excepción ReflectionException si el argumento function contiene una función inválida.

Ejemplos

Ejemplo #1 Ejemplo con ReflectionFunction::__construct()

<?php
/**
 * Un simple contador
 *
 * @return    int
 */
function counter1()
{
    static $c = 0;
    return ++$c;
}

/**
 * Otro simple contador
 *
 * @return    int
 */
$counter2 = function()
{
    static $d = 0;
    return ++$d;

};

function dumpReflectionFunction($func)
{
    // Muestra información básica
    printf(
        "\n\n===> La función '%s' '%s'\n".
        "     declarada en %s\n".
        "     líneas %d a %d\n",
        $func->isInternal() ? 'interna' : 'definida por el usuario',
        $func->getName(),
        $func->getFileName(),
        $func->getStartLine(),
        $func->getEndline()
    );

    // Muestra los comentarios de documentación
    printf("---> Documentación:\n %s\n", var_export($func->getDocComment(), true));

    // Muestra las variables estáticas existentes
    if ($statics = $func->getStaticVariables())
    {
        printf("---> Variables estáticas: %s\n", var_export($statics, true));
    }
}

// Crear una instancia de la clase ReflectionFunction
dumpReflectionFunction(new ReflectionFunction('counter1'));
dumpReflectionFunction(new ReflectionFunction($counter2));
?>

Resultado del ejemplo anterior es similar a:

===> La función definida por el usuario 'counter1'
     declarada en Z:\reflectcounter.php
     líneas 7 a 11
---> Documentación:
 '/**
 * A simple counter
 *
 * @return    int
 */'
---> Variables estáticas: array (
  'c' => 0,
)


===> La función definida por el usuario '{closure}'
     declarada en Z:\reflectcounter.php
     líneas 18 a 23
---> Documentación:
 '/**
 * Another simple counter
 *
 * @return    int
 */'
---> Variables estáticas: array (
  'd' => 0,
)

Ver también