PHP 4.0 Bug #7454: $this in constructor is different than instaniated object
| From: | heathmike at hotmail dot com | Date: | Wed, 25 Oct 2000 16:59:48 +0000 |
| Subject: | PHP 4.0 Bug #7454: $this in constructor is different than instaniated object | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-35928@lists.php.net to get a copy of this message | ||
From: heathmike@hotmail.com
Operating system: RedHat 6.2
PHP version: 4.0.3
PHP Bug Type: *General Issues
Bug description: $this in constructor is different than instaniated object
Trying to store a reference to $this from inside the constructor does not store the same reference
that new class() returns. The following example shows that.
<?php
class foo {
var $Name;
function foo($name) {
Global $List;
$List[] = &$this;
$this->Name = $name;
}
function echoName() {
echo "<br>".$this->Name;
}
}
$bar1 = new foo('constructor1');
$bar2 = new foo('constructor2');
$bar3 = new foo('constructor3');
$bar1->Name = 'outside1';
$bar2->Name = 'outside2';
$bar3->Name = 'outside3';
// $bar1..3 have been changed as shown below
$bar1->echoName();
$bar2->echoName();
$bar3->echoName();
// $list however contains a copy becuase it's values have not been changed
while (list($key, $val) = each($List)) {
$val->echoName();
}
?>
Either a copy is being made when it shouldn't be or I'm doing something wrong. If
I'm doing something wrong I think this should be documented.
Mike Heath
--
Edit Bug report at: http://bugs.php.net/?id=7454&edit=1