Edit report at https://bugs.php.net/bug.php?id=64761&edit=1

 ID:                 64761
 Updated by:         larue...@php.net
 Reported by:        netmosfera at gmail dot com
 Summary:            rebinding of closures doesn't work when they are
                     declared in a static method
-Status:             Open
+Status:             Wont fix
 Type:               Bug
 Package:            *General Issues
 Operating System:   any
 PHP Version:        5.5.0beta4
 Block user comment: N
 Private report:     N

 New Comment:

it's by design.

if you create a closure within a "scope", then the function actually is a 
"METHOD", thus must be bound to current object(this) or static.

for your example, since the closure is create in a static function blah, which 
makes no "this" avaliable while creating closure, then the closure is created 
as 
static ..


Previous Comments:
------------------------------------------------------------------------
[2013-05-02 13:39:28] netmosfera at gmail dot com

Description:
------------
<?

class Test
{
    public $clos;
    
    public $bobo = 22;
    
    static function blah(){
        return new static(function(){ echo $this->bobo; });
    }

    function __construct(\Closure $c)
    {
      $this->clos = $c->bindTo($this, $this);
    }
}

// perfectly fine when closure is declared in global space
$a = new Test(function(){ echo $this->bobo; });
$clos = $a->clos;
$clos();

// binding doesn't work when closure is declared in a static method
$a = Test::blah();
$clos = $a->clos;
$clos();
// results in: Warning: Cannot bind an instance to a static closure




------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=64761&edit=1

Reply via email to