您还可以使用类而不是函数名。只需使用这样的数组
<?php
$reflect = new ReflectionParameter(array('className', 'methodName'), 'property');
?>(PHP 5, PHP 7, PHP 8)
ReflectionParameter::__construct — 构造函数
构造一个ReflectionParameter实例。
示例 #1 使用 ReflectionParameter 类
<?php
函数 foo($a, $b, $c) { }
函数 bar(Exception $a, &$b, $c) { }
函数 baz(ReflectionFunction $a, $b = 1, $c = null) { }
函数 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');
?>