获取函数所需的参数数量
这是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 Reflection,尤其是 ReflectionFunction你的情况。
据我所知,您会发现 getParameters() 也很有用
Use Reflection, especially ReflectionFunction in your case.
As far as I can see you will find getParameters() useful too
唯一的方法是通过反射 https://www.php.net/手册/en/book.reflection.php
Only way is with reflection by going to https://www.php.net/manual/en/book.reflection.php