php 获取函数的参数名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2692481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting function's argument names
提问by Gotys
In PHP Consider this function:
在 PHP 中考虑这个函数:
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');
and it would return:
它会返回:
Array['name','age']
Is this even possible in PHP?
这在PHP中甚至可能吗?
回答by Tom Haigh
You can use Reflection:
您可以使用反射:
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
)
回答by ArtisticPhoenix
This is an old question I happened on looking for something other then Reflection, but I will toss out my current implementation so that it might help someone else. Using array_map
这是我在寻找 Reflection 以外的东西时遇到的一个老问题,但我会放弃我当前的实现,以便它可以帮助其他人。使用 array_map
For a method
对于一个方法
$ReflectionMethod = new \ReflectionMethod($class, $method);
$params = $ReflectionMethod->getParameters();
$paramNames = array_map(function( $item ){
return $item->getName();
}, $params);
For a function
对于一个函数
$ReflectionFunction = new \ReflectionFunction('preg_replace');
$params = $ReflectionFunction->getParameters();
$paramNames = array_map(function( $item ){
return $item->getName();
}, $params);
echo '<pre>';
var_export( $paramNames );
Outputs
输出
array(
'0' => 'regex',
'1' => 'replace',
'2' => 'subject',
'3' => 'limit',
'4' => 'count'
)
Cheers,
干杯,
回答by Lucas Bustamante
It's 2019 and no one said this?
现在是2019年,没人说这个吗?
Just use get_defined_vars():
只需使用get_defined_vars():
class Foo {
public function bar($a, $b) {
var_dump(get_defined_vars());
}
}
(new Foo)->bar('first', 'second');
Result:
结果:
array(2) {
["a"]=>
string(5) "first"
["b"]=>
string(6) "second"
}
回答by RafaSashi
In addition to Tom Haigh's answer if you need to get the default value of optional attributes:
除了 Tom Haigh 的回答之外,如果您需要获取可选属性的默认值:
function function_get_names($funcName){
$attribute_names = [];
if(function_exists($funcName)){
$fx = new ReflectionFunction($funcName);
foreach ($fx->getParameters() as $param){
$attribute_names[$param->name] = NULL;
if ($param->isOptional()){
$attribute_names[$param->name] = $param->getDefaultValue();
}
}
}
return $attribute_names;
}
Useful for variable type validation.
用于变量类型验证。
回答by David Refoua
@Tom Haigh, or do it more classy:
@Tom Haigh,或者做得更优雅:
function getArguments( $funcName ) {
return array_map( function( $parameter ) { return $parameter->name; },
(new ReflectionFunction($funcName))->getParameters() );
}
// var_dump( getArguments('getArguments') );
回答by T.Todua
func_get_args
func_get_args
function my($aaa, $bbb){
var_dump( func_get_args() );
}
my("something","something"); //result: array('aaa' => 'something', 'bbb' => 'something');
also, there exists another global functions: get_defined_vars(), that returns not only function, but all variables.
此外,还存在另一个全局函数:get_defined_vars(),它不仅返回函数,还返回所有变量。

