获取函数需要的参数数量 [英] Get number of parameters a function requires

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

问题描述

这是 PHP传入$ this的扩展问题外面的课

我相信这是我正在寻找的东西,但它在python中而不是php中:

And I believe this is what I'm looking for but it's in python not php: Programmatically determining amount of parameters a function requires - Python

假设我有一个这样的函数:

Let's say I have a function like this:

function client_func($cls, $arg){ }

当我准备调用此函数时,我可能会在伪代码中执行以下操作:

and when I'm ready to call this function I might do something like this in pseudo code:

if function's first parameter equals '$cls', then call client_func(instanceof class, $arg)
else call client_func($arg)

因此,基本上,有没有一种方法可以提前调用函数并查看调用该函数之前需要哪些参数值?

So basically, is there a way to lookahead to a function and see what parameter values are required before calling the function?

我想这就像debug_backtrace(),但是相反.

I guess this would be like debug_backtrace(), but the other way around.

func_get_args()只能在对我无济于事的函数中调用.

func_get_args() can only be called from within a function which doesn't help me here.

有什么想法吗?

推荐答案

使用反射,尤其是ReflectionFunction (针对您的情况).

Use Reflection, especially ReflectionFunction in your case.

$fct = new ReflectionFunction('client_func');
echo $fct->getNumberOfRequiredParameters();

据我所见,您会发现 getParameters()也是有用的

As far as I can see you will find getParameters() useful too

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

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