Re: Getting Default Values via reflection.

From: Date: Thu, 22 May 2008 15:23:03 +0000
Subject: Re: Getting Default Values via reflection.
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-37803@lists.php.net to get a copy of this message
Hi, Please notice that I said "followed by required ones". function foo($a = null, $b) {} The above definition is allowed and the compiler won't complain, but actually that "= null" is completely lost, including in Reflection. Regards, Stan Vassilev
2008/5/22 Stan Vassilev | FM <sv_forums@fmethod.com>:
Hi, Actually PHP ignores default values on parameters followed by required ones. You can't fetch the default value even via Reflection. This is easily detected at compile time so I wonder why the compiler doesn't warn. Regards, Stan Vassilev
I'm not sure this is what you mean, but I use the following function in my debugging logic. <?php function getArgs($s_Function, array $a_PassedParams = array()) { $a_Arguments = array(); if ('' !== $s_Function) { $rf_This = new ReflectionFunction($s_Function); foreach($rf_This->getParameters() as $i_Param => $rp_Param) {
     $a_Arguments[$rp_Param->getName()] = $i_Param <
count($a_PassedParams) ? $a_PassedParams[$i_Param] : $rp_Param->getDefaultValue() . ' (Default)'; } } return $a_Arguments; } function foo($required, $optional = 'snafu', $also_optional = 'sins') { print_r(getArgss(__FUNCTION__, func_get_args())); } foo('fred'); foo('bob', 'jim'); outputs ... Array ( [required] => fred [optional] => snafu (Default) [also_optional] => sins (Default) ) Array ( [required] => bob [optional] => jim [also_optional] => sins (Default) ) -- ----- Richard Quadling Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 "Standing on the shoulders of some very clever giants!" -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.internals (#37803) next »