Edit report at https://bugs.php.net/bug.php?id=64761&edit=1
ID: 64761
Comment by: dave at shax dot com
Reported by: netmosfera at gmail dot com
Summary: rebinding of closures doesn't work when they are
declared in a static method
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:
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.
Previous Comments:
------------------------------------------------------------------------
[2014-03-02 04:21:43] 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 !
------------------------------------------------------------------------
[2013-12-17 20:45:04] 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.
------------------------------------------------------------------------
[2013-10-25 00:00:53] 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-08-15 09:09:03] 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-08-09 09:30:25] 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...
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=64761
--
Edit this bug report at https://bugs.php.net/bug.php?id=64761&edit=1