php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #32745 Incorrect reassignment of $this error
Submitted: 2005-04-18 17:45 UTC Modified: 2006-04-01 16:54 UTC
From: jason at amp-design dot net Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS: Irrelevant
Private report: No CVE-ID: None
 [2005-04-18 17:45 UTC] jason at amp-design dot net
Description:
------------
An incorrect message about the reassignment of $this occurs in the 5.1.x branches of PHP.

The code below doesn't product any errors...


<?php 
class A {}
$a = new A;
$b = &$a;
?>

but the "Reproduce code" produces an error when you are inside class scope, trying to use the & operator to create a reference to $this.

Obviously the code $b = $this; does the exact same thing in PHP5, however this is a problem as existing code (especially stuff written for PHP4 and PHP5) will break. This is certainly the case here as this bug arises in functions/imap_mailbox.php on line 25 of squirrel mail.

Please note this was tested using the php5-200504121430 build which is a couple of days old now. I'm pretty sure it will remain in the latest snapshot.

I have no idea why this comes up with the "reassigment of $this" error. I would have thought this error would happen if the expression was reversed to say $this = &$b.

Reproduce code:
---------------
<?php
class A {
        public function test() {
                $b = &$this;
        }
}
$a = new A;
$a->test();
?>

Expected result:
----------------
No output. $a is created and $b === $a during the scope of A::test() for the given code above.

Actual result:
--------------
Fatal error: Cannot re-assign $this in /var/www/html/test.php on line 4

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-04-18 20:15 UTC] johannes@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

$b = $this already creates an object-reference to $this. 
If you do a $b = &$this PHP couldn\'t detect that $b is the 
same as $this, that could be overwritten and this would 
lead to problems with the engine. 
 [2005-04-19 16:15 UTC] jason at amp-design dot net
Mmmm. You gotta love those generic "Thank you for taking the time to write to us, but this is not a bug" messages, because I have yet to see where it is in the manual ;-)

Anyway besides that, this behaviour has only occured since PHP 5.1. How come it used to work in 5.0.x?

"If you do a $b = &$this PHP couldn't detect that $b is the 
same as $this". OK I can sort of see you have a situation where $b is the same as $this, and therefore in this situation you would be trying to reassign this, which you can not do (much like in C++ that operator= needs a test to make sure you don't assign to ones self as undefined behaviour might occur). However, tell me if I'm just being dumb and not seeing something, but surely you can do an internal check for $b === $this before doing the assignment to compare the object ID's? If $b is aready idendical to $this, you can forget the assignment as there is nothing to do.

What gets me is this used to work in earlier versions of PHP 5.0.x, I can't see why it breaks now as you break backward compatiblity with scripts designed for PHP4 that use the reference operator explictly.

And, oh on a side note, the ' character in your reply in the word "couldn\'t" seems to have been escaped 1 too many times. Maybe that a case for another bug report ;-)
 [2006-04-01 16:54 UTC] gavinfo@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


 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sun Aug 14 07:05:45 2022 UTC