create_function 具有默认参数值?

发布于 2024-08-31 03:44:18 字数 702 浏览 11 评论 0原文

好的,我正在考虑使用 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 技术交流群。

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

发布评论

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

评论(3

绻影浮沉 2024-09-07 03:44:18

考虑以这种方式使用 create_function 创建的函数:

$func = create_function('$who', 'echo "Hello, $who!";');

您可以这样调用它:

$func('World');

您将得到:

Hello, World!

现在,有了参数的默认值,代码可能如下所示:

$func = create_function('$who="World"', 'echo "Hello, $who!";');

注意:我只在传递给 create_function 的第一个参数中添加了参数的默认值。

然后,调用新函数:

$func();

我仍然得到:

Hello, World!

即已使用参数的默认值。

因此,参数的默认值适用于 create_function,就像适用于其他函数一样:您只需将默认值放入参数列表中即可。

之后,关于如何创建包含参数及其值的字符串......我想是几个字符串连接,同时不要忘记转义应该转义的内容。

Considering a function created with create_function this way :

$func = create_function('$who', 'echo "Hello, $who!";');

You can call it like this :

$func('World');

And you'll get :

Hello, World!

Now, having a default value for a parameter, the code could look like this :

$func = create_function('$who="World"', 'echo "Hello, $who!";');

Note : I only added the default value for the parameter, in the first argument passed to create_function.

And, then, calling the new function :

$func();

I still get :

Hello, World!

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.

2024-09-07 03:44:18

你想创建一个匿名函数吗? create_function 用于创建匿名函数。否则你需要正常创建函数,如下所示:

function name($parms)
{
   // your code here
}

如果你想使用create_function,这里是原型:

$newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');
echo "New anonymous function: $newfunc\n";
echo $newfunc(2, M_E) . "\n";
// outputs
// New anonymous function: lambda_1
// ln(2) + ln(2.718281828459) = 1.6931471805599

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:

function name($parms)
{
   // your code here
}

If you want to use the create_function, here is the prototype:

$newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');
echo "New anonymous function: $newfunc\n";
echo $newfunc(2, M_E) . "\n";
// outputs
// New anonymous function: lambda_1
// ln(2) + ln(2.718281828459) = 1.6931471805599
初见终念 2024-09-07 03:44:18

我遇到了同样的问题,试图将数组传递给创建的回调函数...我想我会创建一个临时变量...这很丑陋,但我最好这样做,然后用斜杠折磨自己,我的代码是像现在这样已经足够神秘了。

因此,为了说明:

global $tmp_someArray;
$tmp_someArray = $someArray;
$myCallback = create_function(
    '$arg1',
    '
          global $tmp_someArray;
          // do stuff with $tmp_someArray and $arg1....
          return($something);
    '
);

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:

global $tmp_someArray;
$tmp_someArray = $someArray;
$myCallback = create_function(
    '$arg1',
    '
          global $tmp_someArray;
          // do stuff with $tmp_someArray and $arg1....
          return($something);
    '
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文