ReflectionParameter 类

(PHP 5, PHP 7, PHP 8)

简介

ReflectionParameter 取回了函数或方法参数的相关信息。

要自行检查函数的参数,首先创建一个 ReflectionFunctionReflectionMethod 的实例,然后使用它们的 ReflectionFunctionAbstract::getParameters() 方法来获取参数的数组。

类摘要

ReflectionParameter implements Reflector {
/* 属性 */
public $name ;
/* 方法 */
public allowsNull ( ) : bool
public canBePassedByValue ( ) : bool
final private __clone ( ) : void
public static export ( string $function , string $parameter , bool $return = ? ) : string
public getAttributes ( string|null $name = null , int $flags = 0 ) : array
public getDefaultValue ( ) : mixed
public getDefaultValueConstantName ( ) : string|null
public getName ( ) : string
public getPosition ( ) : int
public getType ( ) : ReflectionType|null
public hasType ( ) : bool
public isArray ( ) : bool
public isCallable ( ) : bool
public isDefaultValueAvailable ( ) : bool
public isDefaultValueConstant ( ) : bool
public isOptional ( ) : bool
public isPassedByReference ( ) : bool
public isVariadic ( ) : bool
public __toString ( ) : string
}

属性

name

参数的名称。只读,在尝试赋值的时候会抛出 ReflectionException

Table of Contents

User Contributed Notes

fgm at riff dot org 11-May-2008 12:44
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 25-Jul-2007 05:53
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 18-Jul-2007 08:58
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"
PHP8中文手册 站长在线 整理 版权归PHP文档组所有