gpt4 book ai didi

php - 获取类函数的参数数量

转载 作者:可可西里 更新时间:2023-11-01 13:11:41 24 4
gpt4 key购买 nike

有没有办法检测类中函数的参数数量?

我想做的是以下内容。

$class = 'foo';
$path = 'path/to/file';
if ( ! file_exists($path)) {
die();
}

require($path);

if ( ! class_exists($class)) {
die();
}

$c = new class;

if (num_function_args($class, $function) == count($url_segments)) {
$c->$function($one, $two, $three);
}

这可能吗?

最佳答案

要获取函数或方法签名中的参数数量,您可以使用

示例

$rf = new ReflectionMethod('DateTime', 'diff');
echo $rf->getNumberOfParameters(); // 2
echo $rf->getNumberOfRequiredParameters(); // 1

要在运行时获取传递给函数的参数数量,您可以使用

示例

function fn() {
return func_num_args();
}
echo fn(1,2,3,4,5,6,7); // 7

关于php - 获取类函数的参数数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3989190/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com