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

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

问题描述

这是 PHP传递$ this到函数的扩展问题外部类

我相信这是我正在寻找,但它在python not php:编程确定参数一个函数需要的数量 - Python

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

假设我有这样的函数:

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

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

据我所见,你会发现 getParameters()也很有用

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

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

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