#34358 [Asn->Csd]: Fatal error: Cannot re-assign $this

From: Date: Fri, 21 Oct 2005 08:07:40 +0000
Subject: #34358 [Asn->Csd]: Fatal error: Cannot re-assign $this
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-86846@lists.php.net to get a copy of this message
 ID:               34358
 Updated by:       dmitry@php.net
 Reported By:      pacha dot shevaev at gmail dot com
-Status:           Assigned
+Status:           Closed
 Bug Type:         Scripting Engine problem
 Operating System: *
 PHP Version:      5.*
 Assigned To:      dmitry
 New Comment:

This bug is fixed.

Disallowing $a = &$this; and foo(&$this) is a feature request (not a
bug). Also I don't see any sense no breake BC and valid code only
because this assignment is dangerous.


Previous Comments:
------------------------------------------------------------------------

[2005-10-04 08:01:48] pacha dot shevaev at gmail dot com

Guys your notes about possible misuse($ref =& $this;$ref = 'foo') are
totally valid but is soooooo much difficult to spot this situation
_when_it_actually_happens_? 

I'm by no means going to use this ref improperly but simply want BC
with PHP4...

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

[2005-10-03 12:42:48] derick@php.net

I'm reopening this so that we can discuss it. I don't think we should
be silently ignoring references as that's something vague. See mail to
internals@ in a bit.

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

[2005-10-03 12:14:59] rasmus@php.net

So for the curious who are wondering what "fixed" means.  The & is
simply ignored in this case now. 

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

[2005-10-03 10:22:47] dmitry@php.net

Fixed in CVS HEAD and PHP_5_1.

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

[2005-10-03 09:40:14] rasmus@php.net

It has everything to do with the new object model.  Take this code:

  $x = &$this;
  $x = 'foo';

For performance reasons we need to catch such references when they
happen in order to prevent the object being overwritten inside itself. 
It would be a bit less confusing if the error happened on the actual
assignment, but in this new object model there $x = &$this; is just as
wrong as $this = &$x since the only purpose one could have for making a
reference to $this itself is if you wanted to change it directly.  In
all valid cases this should be $x = $this.

I suppose an option would be to ignore the reference in this case and
continue on.  That seems to be happening with that last indirect case
you posted, but it would cost us a lookup.

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

The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
    http://bugs.php.net/34358

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


Thread (18 messages)

« previous php.bugs (#86846) next »