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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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

Pull Requests

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: Sat Dec 21 14:01:32 2024 UTC