note 105207 added to reflectionparameter.isdefaultvalueavailable
| From: | php at marcyes dot com | Date: | Wed, 03 Aug 2011 00:37:35 +0000 |
| Subject: | note 105207 added to reflectionparameter.isdefaultvalueavailable | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-182125@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.197
Probable Submitter: 24.143.236.65
----
Manual Page -- http://www.php.net/manual/en/reflectionparameter.isdefaultvalueavailable.php
Edit -- https://master.php.net/note/edit/105207
Del: integrated -- https://master.php.net/note/delete/105207/integrated
Del: useless -- https://master.php.net/note/delete/105207/useless
Del: bad code -- https://master.php.net/note/delete/105207/bad+code
Del: spam -- https://master.php.net/note/delete/105207/spam
Del: non-english -- https://master.php.net/note/delete/105207/non-english
Del: in docs -- https://master.php.net/note/delete/105207/in+docs
Del: other reasons-- https://master.php.net/note/delete/105207
Reject -- https://master.php.net/note/reject/105207
Search -- https://master.php.net/manage/user-notes.php