getting function's argument names

You can use Reflection :

function get_func_argNames($funcName) {
    $f = new ReflectionFunction($funcName);
    $result = array();
    foreach ($f->getParameters() as $param) {
        $result[] = $param->name;   
    }
    return $result;
}

print_r(get_func_argNames('get_func_argNames'));


//output
Array
(
    [0] => funcName
)

It's 2019 and no one said this?

Just use get_defined_vars():

class Foo {
  public function bar($a, $b) {
    var_dump(get_defined_vars());
  }
}

(new Foo)->bar('first', 'second');

Result:

array(2) {
  ["a"]=>
  string(5) "first"
  ["b"]=>
  string(6) "second"
}

This is an old question I happened on looking for something other then Reflection, but I will toss out my current implementation so that it might help someone else. Using array_map

For a method

    $ReflectionMethod =  new \ReflectionMethod($class, $method);

    $params = $ReflectionMethod->getParameters();

    $paramNames = array_map(function( $item ){
        return $item->getName();
    }, $params);

For a function

    $ReflectionFunction =  new \ReflectionFunction('preg_replace');
    $params = $ReflectionFunction->getParameters();
    $paramNames = array_map(function( $item ){
        return $item->getName();
    }, $params);
    echo '<pre>';
    var_export( $paramNames );

Outputs

array(
    '0' => 'regex',
    '1' => 'replace',
    '2' => 'subject',
    '3' => 'limit',
    '4' => 'count'
)

Cheers,

Tags:

Php