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

 ID:                 62135
 User updated by:    Sjon at hortensius dot net
 Reported by:        Sjon at hortensius dot net
 Summary:            Closure->bindTo plus $this reassign = corruption
 Status:             Not a bug
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   ArchLinux
 PHP Version:        5.4.3
 Block user comment: N
 Private report:     N

 New Comment:

Ah, too bad; I should have tested that. Thanks for your explanation!


Previous Comments:
------------------------------------------------------------------------
[2012-05-24 10:02:50] johan...@php.net

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

With the "can not reassign $this" message we try to prevent mistakes. For 
performance and architecture reasons we can't prevent all ways one can shoot in 
one own's foot though. your behavior also isn't closure related:

class A {
  public$a = "public"; 
  function __construct() {
     $x =& $this;
     $x = new stdClass;
     var_dump($this, $this->a);
   } 
}
new A();

will show the same result. The difference between $this and $this->a comes from 
the fact that $this is some magic variable.

------------------------------------------------------------------------
[2012-05-24 09:25:58] Sjon at hortensius dot net

Description:
------------
I just found the bindTo method on a Closure, a started to play with it. I found 
out I can get some serieusly strange behaviour by re-assigning $this. Somehow I 
end up with one variable pointing to different objects?

Test script:
---------------
From: http://3v4l.org/bp598#v540

<?php

class A
{
        public $a = 'public';
}

$m = function (){
        $x =& $this;
        $x = new stdClass;
        var_dump($this, $this->a);
};

$a = new A;
$m = $m->bindTo($a);
$m();

Expected result:
----------------
* Cannot re-assign $this OR
* 'stdClass' + Notice OR
* 'A' + 'public

Actual result:
--------------
object(stdClass)#1 (0) {
}
string(6) "public"


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



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

Reply via email to