您也可以使用類別而不是函式名稱。只需像這樣使用陣列
<?php
$reflect = new ReflectionParameter(array('className', 'methodName'), 'property');
?>
(PHP 5, PHP 7, PHP 8)
ReflectionParameter::__construct — 建構子
建構一個 ReflectionParameter 實例。
範例 #1 使用 ReflectionParameter 類別
<?php
function foo($a, $b, $c) { }
function bar(Exception $a, &$b, $c) { }
function baz(ReflectionFunction $a, $b = 1, $c = null) { }
function abc() { }
$reflect = new ReflectionFunction('foo');
echo $reflect;
foreach ($reflect->getParameters() as $i => $param) {
printf(
"-- 參數 #%d: %s {\n".
" 類別: %s\n".
" 允許 NULL: %s\n".
" 以參考傳遞: %s\n".
" 是否為選用?: %s\n".
"}\n",
$i, // $param->getPosition() 可以使用
$param->getName(),
var_export($param->getClass(), 1),
var_export($param->allowsNull(), 1),
var_export($param->isPassedByReference(), 1),
$param->isOptional() ? 'yes' : 'no'
);
}
?>
上述範例將輸出類似以下的內容
Function [ <user> function foo ] { @@ /Users/philip/cvs/phpdoc/a 2 - 2 - Parameters [3] { Parameter #0 [ <required> $a ] Parameter #1 [ <required> $b ] Parameter #2 [ <required> $c ] } } -- Parameter #0: a { Class: NULL Allows NULL: true Passed to by reference: false Is optional?: no } -- Parameter #1: b { Class: NULL Allows NULL: true Passed to by reference: false Is optional?: no } -- Parameter #2: c { Class: NULL Allows NULL: true Passed to by reference: false Is optional?: no }
您也可以使用類別而不是函式名稱。只需像這樣使用陣列
<?php
$reflect = new ReflectionParameter(array('className', 'methodName'), 'property');
?>