I need to get anonymous function arguments' names.
E.g.:
$func = function ( $param1, $param2 ) { ... };
$names = DO_SOMETHING($func);
// after this $names should become something like array('param1', param2')
Theoretically, it is possible because var_dump($func)
says that $func
is the object of Closure
class and have parameter
property which is array('param1', 'param2')
.
Official documentation at php.net describes no methods of Closure class, which can help me.
I've tried to access this property directly, but PHP died with fatal error: Closure object cannot have properties
.
I've tried to get object vars by get_object_vars
but it seems the parameter
property is declated as private (anyway, get_object_vars
does not return it).
The only one way I know -- to intercept the output of var_dump
and parse it, but as we easily understand this is not the way we should write our scripts =)
Sorry for my bad english.
Can't try this at the moment, but have a look at:
http://www.php.net/manual/en/class.reflectionfunction.php
especially
http://www.php.net/manual/en/reflectionfunctionabstract.getparameters.php
Maybe this will do the trick.
Edit: Try this:
$func = function ( $param1, $param2 ) {
/* some code */
};
$refFunc = new ReflectionFunction($func);
foreach ($refFunc->getParameters() as $refParameter) {
echo $refParameter->getName(), '<br />';
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With