From:             [EMAIL PROTECTED]
Operating system: Linux (redhat)
PHP version:      4.3.0
PHP Bug Type:     Class/Object related
Bug description:  Ability to assign to $this in constructor of class

(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);
print_r($a);

Outputs:
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' 

Thanks,
Matt Friedman.
-- 
Edit bug report at http://bugs.php.net/?id=21380&edit=1
-- 
Try a CVS snapshot:         http://bugs.php.net/fix.php?id=21380&r=trysnapshot
Fixed in CVS:               http://bugs.php.net/fix.php?id=21380&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=21380&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=21380&r=needtrace
Try newer version:          http://bugs.php.net/fix.php?id=21380&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=21380&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=21380&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=21380&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=21380&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=21380&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=21380&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=21380&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=21380&r=isapi
Install GNU Sed:            http://bugs.php.net/fix.php?id=21380&r=gnused

Reply via email to