【问题标题】:Anonymous Function return value匿名函数返回值
【发布时间】:2013-03-29 22:05:47
【问题描述】:

更新:从 PHP7 开始,现在可以使用以下语法来使用匿名函数解引用:

$array[] = [
    'new' => (function()
    {
        ...
        return mt_rand();
    })(),

    'or' => getClosure()()
]

原帖:最近在试验一些东西,想知道有没有办法使用匿名函数的返回值

假设我有一个 for 循环,它创建了一个数组,该数组的每个值都必须有一个数据库调用,我想做的是:

for($i = 0; $i != 10; $i++)
{
    $array[] = [
        'new' => function(){
            // some proccesing here maybe
            // lets use mt_rand for this example.
            return mt_rand();
        },

        'old' => function(){
            return mt_rand();
        }
    ];
}

或许

echo function(){
     // again, we'll just use mt_rand
     return mt_rand();
};

它们都返回一个closure 类。对于上面的示例,是否可以将它们的返回值实际传递回数组或回显?

更新:我已经确定这是不可能的,所以可以在这里找到功能请求:http://bugs.php.net/bug.php?id=64608

【问题讨论】:

  • 你真的需要匿名函数吗?听起来你需要一个普通的,然后在循环中调用它
  • 取决于您拥有的 php 版本...我相信 php 5.4 可能能够做到这一点,但运行旧软件包(即 Debian Squeeze 我知道运行 php 5.3)的服务器不会。跨度>
  • @JulianH.Lam 我的示例在 PHP 5.4.13 上进行了测试
  • @JulianH.Lam 匿名函数自 PHP 5.3 起可用
  • 我已经确定这是不可能的,所以可以在这里找到功能请求:bugs.php.net/bug.php?id=64608

标签: php return anonymous-function


【解决方案1】:

似乎必须先分配闭包,然后才能取消引用 - 试试下面的代码:

for($i = 0; $i != 10; $i++)
{
    $array[] = [
    'new' => call_user_func(function(){
        // some proccesing here maybe
        // lets use mt_rand for this example.
        return mt_rand();
    }),

    'old' => call_user_func(function(){
        return mt_rand();
    })
    ];
}

[编辑] - 修改为使用 call_user_func() 而不是自定义函数 - doh!

【讨论】:

    【解决方案2】:

    尝试将匿名 function 分配给变量。

    $myFunc = function() {
      return 'Test';
    }
    
    echo $myFunc(); // Outputs Test
    

    函数本身的值不是返回值。返回值是函数为called时函数返回的值。

    编辑:

    根据deceze 的建议,您可以使用call_user_func()。实现您想要的另一种方法是使用 php 的eval(),这绝不是一个好的编码习惯。

    $array[] = array(
      'new' => call_user_func(function() {
         // some proccesing here maybe
         // lets use mt_rand for this example.
         return mt_rand();
      }),
      'old' => call_user_func(function() {
        return mt_rand();
      }),
    );
    

    eval()

    echo eval('$x = function() {
      // some proccesing here maybe
      // lets use mt_rand for this example.
      return mt_rand();
    }; return $x();');
    

    【讨论】:

    • 我相信 OP 正在寻找访问 $myFunc 时要返回的值,而不是 $myFunc()
    • @TimCooper 不,你需要函数调用,看这里codepad.viper-7.com/3nnjGW
    • @Sam:是的,我知道您必须评估函数才能获得返回值,但我相信这是您在哪里进行评估的问题。我认为 OP 希望在定义匿名函数时对其进行评估。
    • eval 是最糟糕的解决方案。
    【解决方案3】:

    迄今为止最简单的解决方法:

    echo call_user_func(function () { return 'foo'; });
    

    【讨论】:

    • 匿名函数解引用现在在 PHP7 中可用!
    【解决方案4】:

    您必须将函数分配给一个变量look here

    这个作品

        for($i = 0; $i != 10; $i++)
     {
    $array[] = [
        'new' => function(){
            // some proccesing here maybe
            // lets use mt_rand for this example.
            return mt_rand();
        },
    
        'old' => function(){
            return mt_rand();
        }
    ];
    }
    echo $array[5]['new']();
    

    $function = function(){
     // again, we'll just use mt_rand
     return mt_rand();
    };
    
    echo $function();
    

    【讨论】:

    • 在我的情况下,我需要直接返回,运行$a = function(){ return mt_rand(); }(); 不起作用。
    • 因为是赋值,所以可以$a(),有什么问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    相关资源
    最近更新 更多