ID:               28491
 Updated by:       [EMAIL PROTECTED]
 Reported By:      ladislav dot prosek at matfyz dot cz
-Status:           Open
+Status:           Suspended
 Bug Type:         Class/Object related
 Operating System: Windows XP Pro
 PHP Version:      5.0.0RC2
 New Comment:

Thank you for reprting this issue.  First I'd like to clarify what's
going on:

$this is a very special variable.  In reality it's a combination of
run-time variable and compile-time token.

Direct assignment to $this is resolved at compile time (as opposed to
execution time for all other variables) and is disallowed using the
error message you mentioned.

Creating a reference to $this is resolved at execution time as is any
normal variable assignment (including assignment to a reference to
$this).  While it is possible to perform a check to be sure that the
variable being assigned to is not a reference to $this, that check
would have to be performed for every single assignment.  This'll slow
things down, let's put that in the 'minus' column.

Now, let's look at what indirectly reassigning $this does to the
engine:

When an object property or method is accessed using $this->prop; or
$this->meth();  The object indirection is resolved at compile time
(this ultimately saves time during execution since the object's prop
table  and virtual method table don't need to be repeatedly looked up
at execution time).  This is why var_dump($this); can show the modified
value (a string) while $this->prop; and $this->meth(); still work as
though nothing's changed.  When the method in which $this was
indirectly redefined ends however, $this is simply discarded to be
"recreated" from the calling scope the next time a method is called.

Given the choice between: (A) doing nothing but leaving room for
confusion, (B) taking away the ability to create references, or (C)
checking assignments for references to $this(read: slow);  The current
decision is to go with (A).

This may be revisited prior to the release of PHP 5.1, at which point
there may be a fourth option which avoids the pitfalls of each of the
three previously ennumerated routes, for the time being I'm flagging
this report as 'Suspended'.


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

[2004-05-22 17:19:56] ladislav dot prosek at matfyz dot cz

Description:
------------
Trying to assign to $this inside a method results in:
"Fatal error: Cannot re-assign $this".

However, using a reference it is possible to modify $this.

Reproduce code:
---------------
class A
{
  function f()
  {
    $p =& $this;
    $p = "whatever";

    var_dump($this);

    // Note: accessing fields and invoking methods
    // via $this still works fine.
  }
};

$x = new A();
$x->f();


Expected result:
----------------
Fatal error: Cannot re-assign $this

Actual result:
--------------
string(8) "whatever"


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


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

Reply via email to