Getting Default Values via reflection.

From: Date: Thu, 22 May 2008 11:58:19 +0000
Subject: Getting Default Values via reflection.
Groups: php.internals 
Request: Send a blank email to internals+get-37800@lists.php.net to get a copy of this message
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!"

« previous php.internals (#37800) next »