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 this is not your bug, you can add a comment by following this link.
If this is your bug, but 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

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 Apr 19 03:01:27 2024 UTC