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?

Community
  • 1
  • 1
Taylor Evanson
  • 384
  • 4
  • 16
  • 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 Jul 31 '14 at 16:42
  • Why, out of interest, are you passing $person by reference? – Mark Baker Jul 31 '14 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. – Taylor Evanson Jul 31 '14 at 17:06

1 Answers1

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.

cOle2
  • 4,725
  • 1
  • 24
  • 26