(PHP 5, PHP 7, PHP 8)
ReflectionMethod::__construct — ReflectionMethod 的構(gòu)造函數(shù)
$class_method
)構(gòu)造一個(gè)新的 ReflectionMethod
class
包含方法的類名稱或者這個(gè)類的一個(gè)實(shí)例
name
方法的名稱
class_method
類名稱和方法名稱,之間使用 ::
分隔
沒(méi)有返回值。
如果指定的方法不存在,那么拋出一個(gè) ReflectionException
示例 #1 ReflectionMethod::__construct() example
<?php
class 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 ReflectionMethod class
$method = new ReflectionMethod('Counter', 'increment');
// Print out basic information
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()))
);
// 打印注釋文檔
printf("---> Documentation:\n %s\n", var_export($method->getDocComment(), 1));
// 打印存在的靜態(tài)變量
if ($statics= $method->getStaticVariables()) {
printf("---> Static variables: %s\n", var_export($statics, 1));
}
// 執(zhí)行方法
printf("---> Invocation results in: ");
var_dump($method->invoke(NULL));
?>
以上例程的輸出類似于:
===> 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: '/** * Increment counter * * @final * @static * @access public * @return int */' ---> Invocation results in: int(1)