php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #50806 $this can be assigned using variable casting
Submitted: 2010-01-20 15:48 UTC Modified: 2010-04-20 16:18 UTC
From: kominbhai at gmail dot com Assigned: dmitry (profile)
Status: Wont fix Package: *General Issues
PHP Version: 5.*, 6, 7, 8, 9.. OS: Windows Vista
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: kominbhai at gmail dot com
New email:
PHP Version: OS:

 

 [2010-01-20 15:48 UTC] kominbhai at gmail dot com
Description:
------------
$this can be assigned using variable casting

Reproduce code:
---------------
<?php
$a="this";
$$a="abc";

echo $this;  //works when it should not be
?>

Expected result:
----------------
Error: $this cannot be re-assigned

Actual result:
--------------
echoes abc

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-01-24 04:56 UTC] kalle@php.net
There are several other ways to also re-assign $this, e.g.:

class Test {
 public function __construct() {
  var_dump($this);

  /* Quirk comes in here by using the concation operator */
  /* ${'this'} is not possible and results in an E_ERROR */

  ${'t' . 'his'} = 'Hello';
  var_dump($this);
 }
}

If any whether this should be fixed within the Engine, then Dmitry would know so I'm re-assigning it to him for him to decide. As for a documentation issue, I really don't belive we should document such quirks in the official documentation else people will rely on such buggy "features".
 [2010-04-20 16:18 UTC] dmitry@php.net
-Status: Assigned +Status: Wont fix -Package: Feature/Change Request +Package: *General Issues
 [2010-04-20 16:18 UTC] dmitry@php.net
It's possible to do it even without dynamic tricks.

<?php
class Foo { 
  function bar() { 
    $x = &$this;
    $x = null;
    var_dump($this);
  }
} 
$x = new Foo();
$x->bar();
?>

I would say I don't like to fix all these issues, because the fixes would slowdown absolutely legal PHP code.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 17:01:33 2025 UTC