在 PHP 中动态使用函数原型
我正在用 PHP 编写一个构造,其中解析器确定要动态调用哪个函数,有点像这样:
// The definition of what to call
$function_call_spec = array( "prototype" => "myFunction",
"parameters" => array( "first_par" => "Hello",
"second_par" => "World"));
// Dispatch
$funcPrototype = $function_call_spec["prototype"];
$funcPrototype(); // Here we call function 'myFunction'.
这一切都很好。 但现在是下一步,传递参数,我真的不知道这是否可以按照我想要的方式进行。 然而,现在脚本语言可以做什么,它永远让我感到惊讶,所以这里是:
可以将参数传递给函数,如下所示:
// Here we call function 'myFunction' with the array of parameters.
$funcPrototype( $function_call_spec["parameters"] );
但是,我想用明确的参数正确声明“myFunction”等:
function myFunction( $first_par, $second_par )
{
}
接下来的问题是 - 是有没有办法通过循环参数数组来动态地将参数传递给函数?
澄清一下,我不想这样做:
$funcPrototype( $function_call_spec["parameters"]["first_par"],
$function_call_spec["parameters"]["second_par"] );
因为这需要我的代码静态地了解有关 myFunction 的详细信息,这违背了整个想法。
相反,我可能想以这样的方式执行此操作:
// Special magic PHP function which can be used for invoking functions dynamically
InvokeFunction( $funcPrototype, $function_call_spec["parameters"] );
这会导致 myFunction 被调用,并且数组中的所有参数都会传递到原型中的每个单独的参数变量。
欢迎任何评论。
问候。
/R
PS:本文中的代码均未经过打字错误等测试。
I'm writing a construct in PHP where a parser determins which function to call dynamically, kind of like this:
// The definition of what to call
$function_call_spec = array( "prototype" => "myFunction",
"parameters" => array( "first_par" => "Hello",
"second_par" => "World"));
// Dispatch
$funcPrototype = $function_call_spec["prototype"];
$funcPrototype(); // Here we call function 'myFunction'.
This is all fine and dandy. But now comes the next step, passing the parameters, which I don't really know if it's possible the way I want to do it. It never stops amazing me however what script languages can do these days, so here goes:
One could pass the parameters to the function like this:
// Here we call function 'myFunction' with the array of parameters.
$funcPrototype( $function_call_spec["parameters"] );
However, I want to declare 'myFunction' properly with clear arguments etc:
function myFunction( $first_par, $second_par )
{
}
The question then follows - Is there any way to pass parameters to a function dynamically simply by looping through the parameter array?
To clarify, I don't want to do it like this:
$funcPrototype( $function_call_spec["parameters"]["first_par"],
$function_call_spec["parameters"]["second_par"] );
Because this requires my code to statically know details about myFunction, which goes against the whole idea.
Instead I would want to do it in some way like this maybe:
// Special magic PHP function which can be used for invoking functions dynamically
InvokeFunction( $funcPrototype, $function_call_spec["parameters"] );
Which then results in myFunction being called and all parameters in the array gets passed to each individual parameter variable in the prototype.
Any comments are welcome.
Regards.
/R
PS: None of the code in this post has been tested for typos etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该使用
call_user_func_array
它可以调用任何函数或方法并从数组中获取参数。或者,您可以使用
ReflectionFunction::invokeArgs
,但与call_user_func_array
相比并没有任何优势,除非您已经将此类用于其他用途(例如检查您调用的函数是否接受适当数量和类型的参数)。You should use
call_user_func_array
which can call any function or method and takes parameteres from an array.Alternatively you can use
ReflectionFunction::invokeArgs
, but there's no benefit overcall_user_func_array
unless you already use this class for someting else (like checking whether function you call accepts appropriate number and types of arguments).您可能想要创建一个根据您的喜好命名函数的包装器,例如:
使用此函数,您可以通过 3 种不同的方式调用它:
You might want to create a wrapper that names the function to your preference, such as:
With this function you can call it in 3 different ways:
如果您不需要强制执行合约,则
call_user_func_array()
是最佳选择,否则请使用ReflectionFunction
。call_user_func_array()
is the best choice if you don't need to enforce the contract, otherwise useReflectionFunction
.https://www.php.net/create_function
当您使用 create_function() 时,您的参数不是评估直到运行时。 相当甜蜜。
https://www.php.net/create_function
When you use create_function(), your arguments are not evaluated until runtime. Pretty sweet.