PHP:获取参数计数 [英] PHP: Get argument count

查看:24
本文介绍了PHP:获取参数计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看到 PHP 没有明显函数来完成我正在寻找的功能,这令人难以置信,所以我会在这里问.

This was incredibly surprising to see that PHP has no obvious function to do what I'm looking for, so I'll ask here.

您将如何获取特定函数具有外部的参数数量(就像 func_num_args 仅从 外部函数).

How would you go about getting the number of arguments a particular function has outside of the function (just like func_num_args only from outside the function).

该解决方案实际上无法执行该函数(这将违背目的),我想(最好)在没有任何反射类的情况下执行此操作.

The solution can't actually execute the function (that would be defeating the purpose), and I'd like to do it (preferably) without any sort of reflection class.

可能吗?

推荐答案

哦,你可以使用ReflectionFunction 类,继承自 ReflectionFunctionAbstract,它定义了 getNumberOfParameters 函数:

Oh, you can use the ReflectionFunction class, which inherits from ReflectionFunctionAbstract, which defines the getNumberOfParameters function:

$func_reflection = new ReflectionFunction('function_name');
$num_of_params = $func_reflection->getNumberOfParameters();

注意:这仅适用于用户函数,而不适用于类或实例函数.

Note: This will only work on user functions, not class or instance functions.

这篇关于PHP:获取参数计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆