create_function 具有默认参数值?
好的,我正在考虑使用 create_function
来完成我需要做的事情,但我没有找到用它定义默认参数值的方法。这可能吗?如果是这样,将参数输入 php 中的 create_function 函数的最佳方法是什么?也许使用addslashes
?
好吧,例如,我有一个像这样的函数:
function testing($param1 = 'blah', $param2 = array())
{
if($param1 == 'blah')
return $param1;
else
{
$notblah = '';
if (count($param2) >= 1)
{
foreach($param2 as $param)
$notblah .= $param;
return $notblah;
}
else
return 'empty';
}
}
好的,那么我如何使用 create_function 来完成同样的事情,添加参数及其默认值?
问题是,参数来自文本文件以及函数本身。 因此,想知道使用 create_function 的最佳方法以及应该如何解析字符串。
谢谢 :)
Ok, I'm looking into using create_function
for what I need to do, and I don't see a way to define default parameter values with it. Is this possible? If so, what would be the best approach for inputting the params into the create_function
function in php? Perhaps using addslashes
?
Well, for example, I have a function like so:
function testing($param1 = 'blah', $param2 = array())
{
if($param1 == 'blah')
return $param1;
else
{
$notblah = '';
if (count($param2) >= 1)
{
foreach($param2 as $param)
$notblah .= $param;
return $notblah;
}
else
return 'empty';
}
}
Ok, so how would I use create_function
to do the same thing, adding the parameters and their default values?
The thing is, the parameters are coming from a TEXT file, as well as the function itself.
So, wondering on the best approach for this using create_function
and how exactly the string should be parsed.
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
考虑以这种方式使用
create_function
创建的函数:您可以这样调用它:
您将得到:
现在,有了参数的默认值,代码可能如下所示:
注意:我只在传递给 create_function 的第一个参数中添加了参数的默认值。
然后,调用新函数:
我仍然得到:
即已使用参数的默认值。
因此,参数的默认值适用于
create_function
,就像适用于其他函数一样:您只需将默认值放入参数列表中即可。之后,关于如何创建包含参数及其值的字符串......我想是几个字符串连接,同时不要忘记转义应该转义的内容。
Considering a function created with
create_function
this way :You can call it like this :
And you'll get :
Now, having a default value for a parameter, the code could look like this :
Note : I only added the default value for the parameter, in the first argument passed to create_function.
And, then, calling the new function :
I still get :
i.e. the default value for the parameter has been used.
So, default values for parameters work with
create_function
just like they do for other functions : you just have to put the default value in the list of parameters.After that, on how to create the string containing the parameters and their values... A couple of string concatenations, I suppose, without forgetting to escape what should be escaped.
你想创建一个匿名函数吗?
create_function
用于创建匿名函数。否则你需要正常创建函数,如下所示:如果你想使用
create_function
,这里是原型:Do you want to create an anonymous function? The
create_function
is used to create the anonymous functions. Otherwise you need to create function normally like:If you want to use the
create_function
, here is the prototype:我遇到了同样的问题,试图将数组传递给创建的回调函数...我想我会创建一个临时变量...这很丑陋,但我最好这样做,然后用斜杠折磨自己,我的代码是像现在这样已经足够神秘了。
因此,为了说明:
I'm having the same problem, trying to pass an array to a created callback function... I think I'll create a temporary variable... It's ugly but I have better to do then torture myself with slashes, my code is already cryptic enough the way it is now.
So, to illustrate: