ReflectionParameter 类
(PHP 5, PHP 7, PHP 8)
简介
ReflectionParameter 检索函数或方法参数的相关信息。
要自行检查函数的参数,首先创建 ReflectionFunction 或 ReflectionMethod 的实例,然后使用它们的 ReflectionFunctionAbstract::getParameters() 方法来检索参数的数组。
类摘要
/* 属性 */
/* 方法 */
}属性
- name
-
参数名。只读,在尝试赋值的时候会抛出 ReflectionException。
更新日志
版本 | 说明 |
---|---|
8.0.0 | 已移除 ReflectionParameter::export()。 |
目录
- ReflectionParameter::allowsNull — Checks if null is allowed
- ReflectionParameter::canBePassedByValue — Returns whether this parameter can be passed by value
- ReflectionParameter::__clone — Clone
- ReflectionParameter::__construct — Construct
- ReflectionParameter::export — Exports
- ReflectionParameter::getAttributes — Gets Attributes
- ReflectionParameter::getClass — 获取参数的 ReflectionClass 对象或为 null
- ReflectionParameter::getDeclaringClass — Gets declaring class
- ReflectionParameter::getDeclaringFunction — Gets declaring function
- ReflectionParameter::getDefaultValue — Gets default parameter value
- ReflectionParameter::getDefaultValueConstantName — Returns the default value's constant name if default value is constant or null
- ReflectionParameter::getName — Gets parameter name
- ReflectionParameter::getPosition — Gets parameter position
- ReflectionParameter::getType — Gets a parameter's type
- ReflectionParameter::hasType — Checks if parameter has a type
- ReflectionParameter::isArray — Checks if parameter expects an array
- ReflectionParameter::isCallable — Returns whether parameter MUST be callable
- ReflectionParameter::isDefaultValueAvailable — 检查默认是否可用
- ReflectionParameter::isDefaultValueConstant — Returns whether the default value of this parameter is a constant
- ReflectionParameter::isOptional — Checks if optional
- ReflectionParameter::isPassedByReference — Checks if passed by reference
- ReflectionParameter::isPromoted — Checks if a parameter is promoted to a property
- ReflectionParameter::isVariadic — Checks if the parameter is variadic
- ReflectionParameter::__toString — To string
+添加备注
用户贡献的备注 4 notes
fgm at riff dot org ¶
16 years ago
The note about the signature of the ReflectionParameter constructor is actually incomplete, at least in 5.2.5: it is possible to use an integer for the second parameter, and the constructor will use it to return the n-th parameter.
This allows you to obtain proper ReflectionParameter objects even when documenting code from extensions which (strangely enough) define several parameters with the same name. The string-based constructor always returns the first parameter with the matching name, whereas the integer-based constructor correctly returns the n-th parameter.
So, in short, this works:
<?php
// supposing the extension defined something like:
// Some_Class::someMethod($a, $x, $y, $x, $y)
$p = new ReflectionParameter(array('Some_Class', 'someMethod'), 4);
// returns the last parameter, whereas
$p = new ReflectionParameter(array('Some_Class', 'someMethod'), 'y');
// always returns the first $y at position 2
?>
killgecNOFSPAM at gmail dot com ¶
17 years ago
Signature of constructor of ReflectionParameter correctly is:
public function __construct(array/string $function, string $name);
where $function is either a name of a global function, or a class/method name pair.
massimo at mmware dot it ¶
17 years ago
I found these limitations using class ReflectionParameter from ReflectionFunction with INTERNAL FUNCTIONS (eg print_r, str_replace, ... ) :
1. parameter names don't match with manual: (try example 19.35 with arg "call_user_func" )
2. some functions (eg PCRE function, preg_match etc) have EMPTY parameter names
3. calling getDefaultValue on Parameters will result in Exception "Cannot determine default value for internal functions"
rasmus at mindplay dot dk ¶
1 year ago
There are so many parameter modes now, and I needed to know exactly what `ReflectionParameter` is going to return, so I wrote a little test-script - you can find the script and results in a table here:
https://gist.github.com/mindplay-dk/082458088988e32256a827f9b7491e17
备份地址:http://www.lvesu.com/blog/php/class.reflectionparameter.php