La classe ReflectionParameter

(PHP 5, PHP 7)

Introduction

La classe ReflectionParameter récupère les informations sur les paramètres des fonctions ou des méthodes.

Pour introspecter les paramètres des fonctions, tout d'abord créez une instance de la classe ReflectionFunction ou de la classe ReflectionMethod puis utilisez la méthode ReflectionFunctionAbstract::getParameters() pour récupérer un tableau des paramètres.

Synopsis de la classe

ReflectionParameter implements Reflector {
/* Propriétés */
public $name ;
/* Méthodes */
public allowsNull ( void ) : bool
public canBePassedByValue ( void ) : bool
final private __clone ( void ) : void
public __construct ( string $function , string $parameter )
public static export ( string $function , string $parameter [, bool $return ] ) : string
public getClass ( void ) : ReflectionClass
public getDefaultValue ( void ) : mixed
public getDefaultValueConstantName ( void ) : string
public getName ( void ) : string
public getPosition ( void ) : int
public getType ( void ) : ReflectionType
public hasType ( void ) : bool
public isArray ( void ) : bool
public isCallable ( void ) : bool
public isDefaultValueAvailable ( void ) : bool
public isDefaultValueConstant ( void ) : bool
public isOptional ( void ) : bool
public isPassedByReference ( void ) : bool
public isVariadic ( void ) : bool
public __toString ( void ) : string
}

Propriétés

name

Nom du paramètre. En lecture seule, émets ReflectionException lors d'une tentative d'écriture.

Sommaire

add a note add a note

User Contributed Notes 3 notes

up
8
fgm at riff dot org
11 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
1
killgecNOFSPAM at gmail dot com
12 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
massimo at mmware dot it
12 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