PHP 8.5.8 Released!

ReflectionMethod::__construct

(PHP 5, PHP 7, PHP 8)

ReflectionMethod::__construct构造 ReflectionMethod

说明

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

替代签名(不支持命名参数):

public function ReflectionMethod::__construct(string $classMethod)
警告

从 PHP 8.4.0 开始,替代签名已被弃用,请使用 ReflectionMethod::createFromMethodName() 代替。

构造新的 ReflectionMethod

参数

objectOrMethod

包含方法的类名或者对象(类的实例)。

method

方法名。

classMethod

类名称和方法名称,通过 :: 分隔

错误/异常

如果指定的方法不存在,那么抛出 ReflectionException

示例

示例 #1 ReflectionMethod::__construct() 示例

<?php
class Counter
{
 private static $c = 0;
 /**
 * Increment counter
 *
 * @final
 * @static
 * @access public
 * @return int
 */
 final public static function increment()
 {
 return ++self::$c;
 }
}
// 创建 ReflectionMethod 类的实例
$method = new ReflectionMethod('Counter', 'increment');
// 打印出基本信息
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(), true));
// 打印存在的静态变量
if ($statics= $method->getStaticVariables()) {
 printf("---> Static variables: %s\n", var_export($statics, true));
}
// 执行方法
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)

参见

发现了问题?

了解如何改进此页面提交拉取请求报告一个错误
+添加备注

用户贡献的备注

此页面尚无用户贡献的备注。

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