PHP 7.2.0 Beta 1 Released


(PHP 5 >= 5.0.3, PHP 7)

ReflectionParameter::isDefaultValueAvailableChecks if a default value is available


public bool ReflectionParameter::isDefaultValueAvailable ( void )

Checks if a default value for the parameter is available.


Această funcție nu are parametri.

Valorile întoarse

TRUE if a default value is available, otherwise FALSE

A se vedea și

add a note add a note

User Contributed Notes 1 note

php at marcyes dot com
5 years ago
A quick gotcha that I wasn't aware of, suppose you have a function definition like this:

function foo(array $bar = array('baz' => ''),$che){}

And you want to check if $bar has a default value:

= new ReflectionFunction('foo');
$rparams = $rfunc->getParams();

$rparams[0]->isDefaultValueAvailable() ? 'TRUE' : 'FALSE';

That will echo 'FALSE' because $che has no default value so $bar becomes required and the Reflection interface no long sees $bar's default value of array('baz' => '').

The solution is to give $che a default value also:

function foo(array $bar = array('baz' => ''),$che = null){}

And then $bar's default value will be visible again.

While I understand why it does this, I still wish there was a way to get the default value without resorting to giving all params after it a default value also.
To Top