IAmJulianAcosta IAmJulianAcosta - 5 months ago 19
PHP Question

Modify closure in PHP

Is possible to modify (add more code) to a closure in PHP after is passed to a parameter?

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

Answer

No, you can not add or modify a closure, but you can create another closure which calls this closure:

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