phpday 2027 - Call for Papers

Класс ReflectionParameter

(PHP 5, PHP 7, PHP 8)

Введение

Класс ReflectionParameter сообщает информацию о параметрах методов и функций.

Чтобы иметь возможность исследовать аргументы функции, сначала создайте экземпляр класса ReflectionFunction либо ReflectionMethod, а затем используйте его метод ReflectionFunctionAbstract::getParameters() для получения массива аргументов.

Обзор класса

class ReflectionParameter implements Reflector {
/* Свойства */
public string $name;
/* Методы */
public function __construct(string|array|object $function, int|string $param)
public function allowsNull(): bool
public function canBePassedByValue(): bool
private function __clone(): void
public static function export(string $function, string $parameter, bool $return = ?): string
public function getAttributes(?string $name = null, int $flags = 0): array
#[\Deprecated]
public function getClass(): ?ReflectionClass
public function getDefaultValue(): mixed
public function getName(): string
public function getPosition(): int
public function getType(): ?ReflectionType
public function hasType(): bool
#[\Deprecated]
public function isArray(): bool
#[\Deprecated]
public function isCallable(): bool
public function isDefaultValueAvailable(): bool
public function isDefaultValueConstant(): bool
public function isOptional(): bool
public function isPassedByReference(): bool
public function isPromoted(): bool
public function isVariadic(): bool
public function __toString(): string
}

Свойства

name

Имя аргумента. Доступно только для чтения и выбрасывает исключение ReflectionException при попытке записи.

Список изменений

Версия Описание
8.0.0 Метод ReflectionParameter::export() был удалён.

Содержание

Примечания пользователей 4 notes

up
7
fgm at riff dot org
18 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
?>
up
3
killgecNOFSPAM at gmail dot com
19 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.
up
0
rasmus at mindplay dot dk
3 years 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
up
0
massimo at mmware dot it
19 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"
Adding new notes is disabled. Please see the links in the Found A Problem? section.
To Top