php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70874 Closure binding does not work as expected.
Submitted: 2015-11-07 08:10 UTC Modified: 2015-11-07 08:50 UTC
From: info at mbcraft dot it Assigned:
Status: Not a bug Package: Reflection related
PHP Version: 5.5.30 OS: Linux qwqw 3.13.0-66-generic #10
Private report: No CVE-ID: None
 [2015-11-07 08:10 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-11-07 08:31 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2015-11-07 08:31 UTC] 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:50 UTC] info at mbcraft dot it
Yes you are right. Sorry.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 00:01:28 2024 UTC