php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64761 rebinding of closures doesn't work when they are declared in a static method
Submitted: 2013-05-02 13:39 UTC Modified: 2016-03-26 11:49 UTC
Votes:19
Avg. Score:4.5 ± 0.8
Reproduced:16 of 16 (100.0%)
Same Version:5 (31.2%)
Same OS:7 (43.8%)
From: netmosfera at gmail dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5.5.0beta4 OS: any
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2013-05-02 13:39 UTC] 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



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-05-03 15:28 UTC] laruence@php.net
-Status: Open +Status: Wont fix
 [2013-05-03 15:28 UTC] laruence@php.net
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 ..
 [2013-05-03 23:44 UTC] netmosfera at gmail dot com
makes no sense to me!
also in the global scope $this isn't available, but rebind does work!
 [2013-05-03 23:52 UTC] hanskrentel at yahoo dot de
@laruence: This is not by design. Please take a second look on this report. Thank 
you. You probably want to summon ircmaxell for help.
 [2013-05-04 05:07 UTC] laruence@php.net
as I saw, it's by design, and there are some test scripts about that:

https://github.com/php/php-src/blame/master/Zend/zend_closures.c#L499

btw, why should I trun to ircmaxell? he is the author of this?
 [2013-08-09 09:02 UTC] wanwizard at fuelphp dot com
I understand the issue of scope here, but imho the location of where the closure is not relevant. You get the same error when you do:

public static function somemethod()
{
    // construct the a new object
    $instance = static::factory();

    // get the current event object
    $event = \Event::getInstance();

    // setup a shutdown event
    $event->on('shutdown', function($event) { $this->process(); }, $instance);
}

where the "on" method is trying to bind $instance (which is an object, so there should be no issue, $this is defined), but fails with this same error.
 [2013-08-09 09:30 UTC] wanwizard at fuelphp dot com
To illustrate, I now have to this to get it to work:

class Dummy
{
	public function __construct($event, $object)
	{
		// setup a shutdown event for writing cookies
		$event->on('shutdown', function($event) { $this->process(); }, $object);
	}
}

new Dummy($event, $instance);

which does exactly the same, but now the closure is defined in an object context. Which is insane...
 [2013-08-15 09:09 UTC] wanwizard at fuelphp dot com
I've looked into the code, and indeed, it simply bails out when you want to bind an object to the closure and it's initially created as ZEND_ACC_STATIC.

So instead of

if ((newthis != NULL) && (closure->func.common.fn_flags & ZEND_ACC_STATIC)) {
    zend_error(E_WARNING, "Cannot bind an instance to a static closure");
}

it should change the scope of the closure (as the bindTo will provide an object to bind to, which implicitly changes the context of the closure. Since the closure is effectively recreated, it shouldn't be an issue changing the scope while doing that.
 [2013-10-25 00:00 UTC] agita at live dot de
I really don't understand why the closure is expected to be static just because its declared within a static method. For example I'm creating an instance within a method and binding a closure to it. for some reasons i had to change the method to static and now it doesn't work anymore. as workaround i have to define a global function that i can call an that creates and binds the closure. but that's just dumb ...
 [2013-12-17 20:45 UTC] php at maisqi dot com
This behavior is counter-intuitive and I doubt there's a single other language that has such a similar artificial constraint. PLease reconsider fixing this, or at least allow us to contribute a patch to this.
 [2014-03-02 04:21 UTC] sjaillet at gmail dot com
What's the point of providing a `bindTo()` function if it's not for binding a closure to what the developper needs to bind it to ?

Without any solid argument, it's clearly a BUGS !
 [2014-05-16 11:14 UTC] dave at shax dot com
I'm hitting this all the time. Very annoying.

I understand why the original Closure has static scope.

But if I'm using bindTo(), I'm explicitly saying I WANT the closure to have object scope now, The restriction seems arbitrary.
 [2016-03-26 09:19 UTC] Cpuidle at gmx dot de
+1 for getting this fixed. At least the bindTo() behaviour is crazy.
 [2016-03-26 11:49 UTC] nikic@php.net
@cpuidle: This bug has already been fixed in PHP 7.0.
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sat Aug 19 14:01:35 2017 UTC