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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: info at mbcraft dot it
New email:
PHP Version: OS:

 

 [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-2020 The PHP Group
All rights reserved.
Last updated: Fri Apr 10 09:01:23 2020 UTC