【问题标题】:Modify closure in PHP在 PHP 中修改闭包
【发布时间】:2016-10-28 07:14:18
【问题描述】:

是否可以在传递给参数后修改(添加更多代码)到 PHP 中的闭包?

function foo () {
    bar (
        function () {
             $a = baz ();
             $b = fooBar ();
        }
    );
}
function bar (Closure $closure) {
    $closure // <------ I need to add code to this function.
}

【问题讨论】:

    标签: php closures


    【解决方案1】:

    不,你不能添加或修改一个闭包,但你可以创建另一个调用这个闭包的闭包:

    public function getClosure() {
        $closure = getOtherClosure();
        return function() use ($closure) {
            //Do other things
            $closure();
        };
    }
    

    【讨论】:

      猜你喜欢
      • 2017-07-07
      • 1970-01-01
      • 2016-05-31
      • 2011-09-29
      • 2011-12-14
      • 2011-07-28
      • 2010-09-23
      • 2015-11-08
      相关资源
      最近更新 更多