downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

ReflectionParameter::isOptional> <ReflectionParameter::isArray
[edit] Last updated: Fri, 25 May 2012

view this page in

ReflectionParameter::isDefaultValueAvailable

(PHP 5 >= 5.0.3)

ReflectionParameter::isDefaultValueAvailableデフォルト値が存在するかどうかを調べる

説明

public bool ReflectionParameter::isDefaultValueAvailable ( void )

パラメータのデフォルト値が存在するかどうかを調べます。

パラメータ

この関数にはパラメータはありません。

返り値

デフォルト値が存在する場合に TRUE、それ以外の場合に FALSE を返します。

参考



add a note add a note User Contributed Notes ReflectionParameter::isDefaultValueAvailable
php at marcyes dot com 03-Aug-2011 02:37
A quick gotcha that I wasn't aware of, suppose you have a function definition like this:

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

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

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

echo
$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:

<?php
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.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites