2

Possible Duplicate:
Use variables inside an anonymous function, which is defined somewhere else

In PHP >= 5.4 we have a Closure class with the bind or bindTo methods to bind an anonymous function to an object, so that the closure has access to the $this variable, as if it was defined inside that class.

A simple example:

<?php
$closure = function() {
    echo $this->bla; //prints "bbb"
};

class A
{
    public $bla = 'bbb';
    public function blaat($closure)
    {
        $someVar = 'something';

        $bc = Closure::bind($closure, $this, 'A');
        $bc();
    }
}

$a = new A();
$a->blaat($closure);

That is all very nice and all, but what about the use keyword..?

When i rebind a closure inside the method of a class, then i probably want the closure to have access to the "locally" defined variables aswell, so its parent scope. For example the $someVar variable in the above example. But the bind method of the Closure class doesn't care about the use keywords or the abillity to use variables from the parent scope.

It feels somewhat incomplete this way... Anyone any idea if this ever gets supported, or perhaps if there is already a way to use the parent scoped variables when redefining a closure with the bind or bindTo method?

4

0 回答 0