PHP, Tokenizer, find all the arguments of the function
Help me find all the a开发者_高级运维rguments of the function "funcname" using the function token_get_all() in the source code. It sounds simple, but there are many special options, such as arrays as parameters or call static methods as parameters. Maybe there's a simple universal solution?
UPD:
I need the function arguments passed when you call it. Get them to be at an external analysis of the file. For example, there is a php-file:
<?php
funcname('foo');
funcname(array('foo'), 'bar');
The analyzer should begin as follows:
$source = file_get_contents('source.php');
$tokens = token_get_all($source);
...
As a result, need to get a list like this:
[0] => array('foo'),
[1] => array(array('foo'), 'bar')
Rather than using a tokenizer, use reflection. In this case, use ReflectionFunction
:
function funcname ($foo, $bar) {
}
$f = new ReflectionFunction('funcname');
foreach ($f->getParameters() as $p) {
echo $p->getName(), "\n";
}
This outputs
foo
bar
You can also use this class and related classes (such as ReflectionParameter
) to find out more information about a function and its parameters, such as whether a parameter is optional and what its default value is.
精彩评论