Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
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 –  Mark Baker 19 hours ago
    
Why, out of interest, are you passing $person by reference? –  Mark Baker 19 hours ago
    
I didn't specify; this is a fake function. As it is written, there would be no reason to pass $person by reference. –  Taylor Evanson 19 hours ago

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
Seriously, PERFECT. Thank you so much! –  Taylor Evanson 19 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.