获取函数所需的参数数量

发布于 2024-12-10 04:05:45 字数 806 浏览 3 评论 0原文

这是PHP pass in $this to function external class<的扩展问题/a>

我相信这就是我正在寻找的东西,但它是在 python 中而不是 php: 以编程方式确定函数所需的参数数量 - Python

比方说我有一个这样的函数:

function client_func($cls, $arg){ }

当我准备好调用这个函数时,我可能会在伪代码中执行类似的操作:

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

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

我想这就像 debug_backtrace() 一样,但相反。

func_get_args() 只能从函数内部调用,这对我没有帮助。

有什么想法吗?

This is an extension question of PHP pass in $this to function outside class

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?

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

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

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你不是我要的菜∠ 2024-12-17 04:05:45

使用 Reflection,尤其是 ReflectionFunction你的情况。

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

据我所知,您会发现 getParameters() 也很有用

Use Reflection, especially ReflectionFunction in your case.

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

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

愿得七秒忆 2024-12-17 04:05:45

唯一的方法是通过反射 https://www.php.net/手册/en/book.reflection.php

class foo {
 function bar ($arg1, $arg2) {
   // ...
 }
}

$method = new ReflectionMethod('foo', 'bar');
$num = $method->getNumberOfParameters();

Only way is with reflection by going to https://www.php.net/manual/en/book.reflection.php

class foo {
 function bar ($arg1, $arg2) {
   // ...
 }
}

$method = new ReflectionMethod('foo', 'bar');
$num = $method->getNumberOfParameters();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文