PHP Conference Japan 2024

ReflectionMethod::__construct

(PHP 5, PHP 7, PHP 8)

ReflectionMethod::__construct建構 ReflectionMethod

說明

public ReflectionMethod::__construct(物件|字串 $objectOrMethod, 字串 $method)

替代簽章(不支援具名引數)

public ReflectionMethod::__construct(字串 $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;
}
}

// 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()))
);

// Print documentation comment
printf("---> Documentation:\n %s\n", var_export($method->getDocComment(), true));

// Print static variables if existant
if ($statics= $method->getStaticVariables()) {
printf("---> Static variables: %s\n", var_export($statics, true));
}

// Invoke the method
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)

另請參見

新增註釋

使用者貢獻的註釋

此頁面沒有使用者貢獻的註釋。
To Top