Dutch PHP Conference 2023 - Call for Papers

ReflectionParameter クラス

(PHP 5, PHP 7, PHP 8)


ReflectionParameter クラスは、 関数またはメソッドのパラメータに関する情報を取得します。

関数パラメータの内部を調べる際には、まず ReflectionFunction クラスまたは ReflectionMethod クラスのインスタンスを作成する必要があります。次に、 ReflectionFunctionAbstract::getParameters() メソッドを使ってパラメータの配列を取得します。


class ReflectionParameter implements Reflector {
/* プロパティ */
public string $name;
/* メソッド */
public __construct(string|array|object $function, int|string $param)
public allowsNull(): bool
public canBePassedByValue(): bool
private __clone(): void
public static export(string $function, string $parameter, bool $return = ?): string
public getAttributes(?string $name = null, int $flags = 0): array
public getDefaultValueConstantName(): ?string
public getName(): string
public getPosition(): int
public hasType(): bool
public isArray(): bool
public isCallable(): bool
public isDefaultValueConstant(): bool
public isOptional(): bool
public isPassedByReference(): bool
public isVariadic(): bool
public __toString(): string



パラメータ名。読み込み専用で、書き込もうとすると ReflectionException をスローします。


バージョン 説明
8.0.0 ReflectionParameter::export() は、削除されました。


add a note

User Contributed Notes 3 notes

fgm at riff dot org
15 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:
// 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
15 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
15 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"
To Top