PHP 8.3.7 Released!

ReflectionMethod::__construct

(PHP 5, PHP 7, PHP 8)

ReflectionMethod::__constructConstrói um ReflectionMethod

Descrição

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

Alternative signature (not supported with named arguments):

public ReflectionMethod::__construct(string $classMethod)

Constrói um novo ReflectionMethod.

Parâmetros

objectOrMethod

Nome da classe ou objeto (instância da classe) que contém o método.

method

Nome do método.

classMethod

Nome da classe e nome do método delimitados por ::.

Erros/Exceções

Um ReflectionException será lançado se o método fornecido não existir.

Exemplos

Exemplo #1 Exemplo de ReflectionMethod::__construct()

<?php
class Counter
{
private static
$c = 0;

/**
* Contador de incremento
*
* @final
* @static
* @access public
* @return int
*/
final public static function increment()
{
return ++
self::$c;
}
}

// Cria uma instância da classe ReflectionMethod
$method = new ReflectionMethod('Counter', 'increment');

// Imprime informações básicas
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()))
);

// Imprime comentário da documentação
printf("---> Documentation:\n %s\n", var_export($method->getDocComment(), true));

// Imprime variáveis estáticas se existirem
if ($statics= $method->getStaticVariables()) {
printf("---> Static variables: %s\n", var_export($statics, true));
}

// Invoca o método
printf("---> Invocation results in: ");
var_dump($method->invoke(NULL));
?>

O exemplo acima produzirá algo semelhante 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 de incremento
     *
     * @final
     * @static
     * @access  public
     * @return  int
     */'
---> Invocation results in: int(1)

Veja Também

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top