MichaelLuthor
MichaelLuthor

Reputation: 436

how can I get the arguments list of a method from another function under PHP?

If I have a class like this :

class A {
    public function method1($arg1, $arg2){}
}

and now, I need to do something like this :

/**
 * @return array The list of arguemnt names
 */
function getMethodArgList(){
    return get_method_arg_list(A, method1);
}

so, how could I implement the function getMethodArgList() ? any one could help me ?

Upvotes: 0

Views: 52

Answers (1)

VolkerK
VolkerK

Reputation: 96159

Not quite sure if I get the question, but ReflectionClass and ReflectionMethod might be what you're looking for.

e.g.

<?php
var_dump(getMethodArgList());

class A {
    public function method1($arg1, $arg2){}
}

function getMethodArgList() {
    $rc = new ReflectionClass('A');
    $rm = $rc->getMethod('method1');
    return $rm->getParameters();
}

prints

array(2) {
  [0] =>
  class ReflectionParameter#3 (1) {
    public $name =>
    string(4) "arg1"
  }
  [1] =>
  class ReflectionParameter#4 (1) {
    public $name =>
    string(4) "arg2"
  }
}

To get only the names you can use

return array_map(function($e) { return $e->getName(); }, $rm->getParameters());

instead of return $rm->getParameters();

Upvotes: 4

Related Questions