|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #21380 Ability to assign to $this in constructor of class
Submitted: 2003-01-02 22:20 UTC Modified: 2003-01-26 17:28 UTC
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: matt-php at sprynewmedia dot com Assigned:
Status: Closed Package: Class/Object related
PHP Version: 4.3.0 OS: Linux (redhat)
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: matt-php at sprynewmedia dot com
New email:
PHP Version: OS:


 [2003-01-02 22:20 UTC] matt-php at sprynewmedia dot com
(Stig asked me to report this.) Here's the code and the explanation will follow:
class Error {
  var $e = '';
  function Error($msg) {
      $this->e = $msg;

class Foo {
  var $n;
  function &Foo($n) {
    if ($n < 0) {
      // note use of assignment operator "="
      $this = new Error('Foo does not like that
                            negative stuff');
    } else {
        $this->n = $n;

$a = new Foo(-1);

error Object
    [e] => Foo does not like that negative stuff
Currently, if one assignes to $this in the constructor using the assignment operator (=) the $this reference will be overwritten and so the constructor will return something other than the object requested by the new operator.

This does not appear to apply to the assign by reference operator (=&) that is, it doesn't appear to be possible to overwrite the reference in $this with another reference. However, using "=" allows one to set $this to anything; it doesn't even have to be an object and that item will be returned from the new operator via the constructor.

The question is, is this a feature, and if so, will it continue to be so? Or, is it a bug? If it's a feature it makes returning a PEAR error object quite simple and furthermore I have seen some PEAR code (can't remember where exactly) that uses this feature so if it is a bug that will have to be fixed too. I would like to know if this can be relied upon into the future.

'./configure' '--prefix=/usr/local' '--with-pspell' '--with-imap' '--with-imap-ssl' '--with-gettext' '--with-apache=/usr/local/src/apache_1.3.27' '--with-mysql=/usr' '--enable-force-cgi-redirect' '--enable-bcmath' '--enable-discard-path' '--enable-pear' '--with-kerberos' '--with-zlib' '--enable-ftp' '--with-mm' '--enable-force-cgi-redirect' 

Matt Friedman.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2003-01-02 22:26 UTC] matt-php at sprynewmedia dot com
Certainly not a big deal but it was Jesus M. Castagnetto that asked me to report this, not Stig. Thanks.
 [2003-01-26 17:28 UTC]
Word on the street is that this is an unintentional feature 
(a bug).  Under ZE2 (i.e. moving forward) reassignment of 
$this will not be supported.  
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 14 18:01:30 2024 UTC