#34358 [Bgs->Csd]: Fatal error: Cannot re-assign $this(again)

From: Date: Mon, 03 Oct 2005 08:22:49 +0000
Subject: #34358 [Bgs->Csd]: Fatal error: Cannot re-assign $this(again)
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-85919@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:           Bogus
+Status:           Closed
 Bug Type:         Scripting Engine problem
 Operating System: *
 PHP Version:      5.*
-Assigned To:      
+Assigned To:      dmitry
 New Comment:

Fixed in CVS HEAD and PHP_5_1.


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

[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.

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

[2005-09-15 15:32:13] pacha dot shevaev at gmail dot com

And why does the following code work then???

<?php

class Foo {
  function Foo() {
    $this->ref =& $this;
    $this->ref->test();
  }

  function test() {
    echo 'test';
  }
}

$foo = new Foo();

?>

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

[2005-09-15 15:28:02] derick@php.net

It's still bogus, you can call it a BC break if you want, but we're not
going to change this back.

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

[2005-09-15 15:23:37] pacha dot shevaev at gmail dot com

I'm not an expert in PHP internals but there's a guy(stereofrog) on the
SitePoint forum who has a different point of
view(http://www.sitepoint.com/forums/showpost.php?p=2146304&postcount=9):

[quote]
it's 'simply ridiculous. They have a code in zend_compile.c that
handles "$this=$x" and copy-pasted that code in the function that
comples assignment by reference. This should prevent "$this=&$x" (which
is wrong), but for some reason it "prevents" "$x=&$this" as well (which
is absolutely correct). It's pure c-level bug and has nothing to do
with "new object model" and other blah-blah.
[/quote]

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

[2005-09-15 14:27:03] sniper@php.net

NOTE: This is about PHP 5. It might have worked in PHP 4 but it does
not and will not work in PHP 5.

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

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 (#85919) next »