获取函数的参数名称 [英] getting function's argument names
本文介绍了获取函数的参数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在PHP中,请考虑以下功能:
In PHP Consider this function:
function test($name, $age) {}
我需要以某种方式提取参数名称(用于自动生成自定义文档),以便可以执行以下操作:
I need to somehow extract the parameter names (for generating custom documentations automatically) so that I could do something like:
get_func_argNames('test');
,它将返回:
Array['name','age']
在PHP中甚至有可能吗?
Is this even possible in PHP?
推荐答案
您可以使用反射:
function get_func_argNames($funcName) {
$f = new ReflectionFunction($funcName);
$result = array();
foreach ($f->getParameters() as $param) {
$result[] = $param->name;
}
return $result;
}
print_r(get_func_argNames('get_func_argNames'));
//output
Array
(
[0] => funcName
)
这篇关于获取函数的参数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文