Bug #70874 [Nab]: Closure binding does not work as expected.

From: Date: Sat, 07 Nov 2015 08:50:39 +0000
Subject: Bug #70874 [Nab]: Closure binding does not work as expected.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-197094@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70874&edit=1

 ID:                 70874
 User updated by:    info at mbcraft dot it
 Reported by:        info at mbcraft dot it
 Summary:            Closure binding does not work as expected.
 Status:             Not a bug
 Type:               Bug
 Package:            Reflection related
 Operating System:   Linux qwqw 3.13.0-66-generic #10
 PHP Version:        5.5.30
 Block user comment: N
 Private report:     N

 New Comment:

Yes you are right. Sorry.


Previous Comments:
------------------------------------------------------------------------
[2015-11-07 08:31:18] nikic@php.net

bindTo() returns a new closure, so you have to write $fn = $fn->bindTo($a2) to make this work.

------------------------------------------------------------------------
[2015-11-07 08:10:00] info at mbcraft dot it

Description:
------------
The closure is simply not rebound as described by the language specification using the bindTo
function.

Test script:
---------------
<?php

class A {
    private $my_string;
    public function __construct($st) {
        $this->my_string = $st;
    }
    public function launch() {
        return function() {
            echo $this->my_string."\n";
        };
    }
}
$a = new A("go");
$fn = $a->launch();
$fn();
$a2 = new A("ppppp");
$fn->bindTo($a2);
$fn();

Expected result:
----------------
go
ppppp

Actual result:
--------------
go
go


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



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


Thread (3 messages)

« previous php.bugs (#197094) next »