The ReflectionParameter class

(PHP 5)

Introduction

The ReflectionParameter class retrieves information about function's or method's parameters.

To introspect function parameters, first create an instance of the ReflectionFunction or ReflectionMethod classes and then use their ReflectionFunctionAbstract::getParameters() method to retrieve an array of parameters.

Class synopsis

ReflectionParameter implements Reflector {
/* Properties */
public $name ;
/* Methods */
public bool allowsNull ( void )
public bool canBePassedByValue ( void )
final private void __clone ( void )
public __construct ( string $function , string $parameter )
public static string export ( string $function , string $parameter [, bool $return ] )
public ReflectionClass getClass ( void )
public ReflectionClass getDeclaringClass ( void )
public ReflectionFunctionAbstract getDeclaringFunction ( void )
public mixed getDefaultValue ( void )
public string getDefaultValueConstantName ( void )
public string getName ( void )
public int getPosition ( void )
public bool isArray ( void )
public bool isCallable ( void )
public bool isDefaultValueAvailable ( void )
public bool isDefaultValueConstant ( void )
public bool isOptional ( void )
public bool isPassedByReference ( void )
public string __toString ( void )
}

Properties

name

Name of the parameter. Read-only, throws ReflectionException in attempt to write.

Table of Contents

Коментарии

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"
2007-07-18 11:58:55
http://php5.kiev.ua/manual/ru/class.reflectionparameter.html
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.
2007-07-25 08:53:29
http://php5.kiev.ua/manual/ru/class.reflectionparameter.html
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
?>
2008-05-11 15:44:32
http://php5.kiev.ua/manual/ru/class.reflectionparameter.html
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
2023-10-11 13:55:49
http://php5.kiev.ua/manual/ru/class.reflectionparameter.html

    Поддержать сайт на родительском проекте КГБ