# Getting Default Values via reflection.

- Thread: https://externals.io/message/37800
- Original archive: https://news-web.php.net/php.internals/37800
- Started by Richard Quadling on 2008-05-22T11:58:19+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #37800 — Richard Quadling — 2008-05-22T11:58:19+00:00

URL: https://externals.io/message/37800#37800

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!"

---

## #37803 — Stan Vassilev | FM — 2008-05-22T15:23:03+00:00

URL: https://externals.io/message/37800#37803 · In reply to: #37800

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>:
> > […]
>
> 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!"

---

## #37809 — Lars Strojny — 2008-05-22T22:14:41+00:00

URL: https://externals.io/message/37800#37809 · In reply to: #37803

Hi,

Am Donnerstag, den 22.05.2008, 18:23 +0300 schrieb Stan Vassilev | FM:
[...]
> function foo($a = null, $b) {}

Isn't that a typical case for throwing an `E_STRICT` warning or is is not
possible to check that efficiently in the engine?

cu, Lars
