PHP 8.6.0 Beta 1 is available for testing

ReflectionMethod::__construct

(PHP 5, PHP 7, PHP 8)

ReflectionMethod::__constructConstruye un nuevo objeto ReflectionMethod

Descripción

public function ReflectionMethod::__construct(object |string $objectOrMethod, string $method)

Firma alternativa (no soportada con argumentos nombrados):

public function ReflectionMethod::__construct(string $classMethod)
Advertencia

La firma alternativa está obsoleta a partir de PHP 8.4.0, utilice ReflectionMethod::createFromMethodName() en su lugar.

Construye un nuevo objeto ReflectionMethod .

Parámetros

objectOrMethod

Nombre de clase -o instancia de esta- que contiene el método.

method

Nombre del método.

classMethod

Nombre de clase y de método delimitados por ::.

Errores/Excepciones

Se emite una ReflectionException si el método considerado no existe.

Ejemplos

Ejemplo #1 Ejemplo con ReflectionMethod::__construct()

<?php
class Counter
{
 private static $c = 0;
 /**
 * Contador que se incrementa
 *
 * @final
 * @static
 * @access public
 * @return int
 */
 final public static function increment()
 {
 return ++self::$c;
 }
}
// Crea una nueva instancia de la clase ReflectionMethod
$method = new ReflectionMethod('Counter', 'increment');
// Muestra información
printf(
 "===> 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()))
);
// Muestra el comentario de documentación
printf("---> Documentation:\n %s\n", var_export($method->getDocComment(), true));
// Muestra las variables estáticas, si existen
if ($statics= $method->getStaticVariables()) {
 printf("---> Static variables: %s\n", var_export($statics, true));
}
// Invoca el método
printf("---> Invocation results in: ");
var_dump($method->invoke(NULL));
?>

Resultado del ejemplo anterior es similar a:

===> The user-defined final public static method 'increment' (which is a regular method)
 declared in /Users/philip/cvs/phpdoc/test.php
 lines 14 to 17
 having the modifiers 261[final public static]
---> Documentation:
 '/**
 * Contador que se incrementa
 *
 * @final
 * @static
 * @access public
 * @return int
 */'
---> Invocation results in: int(1)

Véase también

Found A Problem?

Learn How To Improve This PageSubmit a Pull RequestReport a Bug
+add a note

User Contributed Notes

There are no user contributed notes for this page.

AltStyle によって変換されたページ (->オリジナル) /