0

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 ?

1 Answer 1

4

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();

Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much, I was just stuck at $rm = rc->getMethod('method1'); I don't know what else to do after this... anyaway, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.