0

I am building a documentation page for a PHP library. I started with retrieving an array of defined functions from a PHP file, and it's working brilliantly inside of a directory traversing loop.

Now i'm trying to retrieve the arguments that are defined in that function. In this example...

function askQuestion(&$person, $question) {
  if(!$person['nice']){
    return false;
  }
  else {
    return 'Go to college';
  }
}

I would want to retrieve either this string: &$person, $question or an array of each argument somehow? Preferably, i'd want to know if it is a variable by reference like &$person.

I found the func_get_arg(s) documentation but, from what I understand, it's meant to retrieve the arguments of the function that you are calling func_get_args() from, and not the actual argument variable names. As far as research goes, i'm not fundamental enough with PHP yet to know what jargon I should be using to find this answer online. Any ideas?

3
  • You probably want to use a tokenizer to do this.... but if you used docblocks in your code, you could annotate all these functions for phpdocumentor to build your code documentation Commented Jul 31, 2014 at 16:42
  • Why, out of interest, are you passing $person by reference? Commented Jul 31, 2014 at 16:43
  • I didn't specify; this is a fake function. As it is written, there would be no reason to pass $person by reference. Commented Jul 31, 2014 at 17:06

1 Answer 1

1

Take a look at the ReflectionFunction class:

$refFunc = new ReflectionFunction('askQuestion');

$params = array();
foreach($refFunc->getParameters() as $param){
    $params[] = $param->__toString();
}

print_r($params);

Outputs:

Array
(
    [0] => Parameter #0 [ <required> &$person ]
    [1] => Parameter #1 [ <required> $question ]
)

Online demo here.

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

1 Comment

Seriously, PERFECT. Thank you so much!

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.