Search code examples
phpargumentscallable-object

Get PHP callable arguments as an array?


Say I have a callable stored as a variable:

$callable = function($foo = 'bar', $baz = ...) { return...; }

How would I get 'bar'?

if (is_callable($callable)) {
  return func_get_args();
}

Unfortunately func_get_args() is for the current function, is it possible to get a key value pair of arguments?


Solution

  • You can use reflection:

    $f = new ReflectionFunction($callable);
    $params = $f->getParameters();
    echo $params[0]->getDefaultValue();