PHP 4.0 Bug #7455: Problems with $this in constructor
| From: | youngm at effinitylabs dot com | Date: | Wed, 25 Oct 2000 17:09:24 +0000 |
| Subject: | PHP 4.0 Bug #7455: Problems with $this in constructor | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-35933@lists.php.net to get a copy of this message | ||
From: youngm@effinitylabs.com
Operating system: Redhat 7.0
PHP version: 4.0.3
PHP Bug Type: *General Issues
Bug description: Problems with $this in constructor
6529
I have been working on a very reference intensive project and have run into a number of problems.
This is one that I've been able to pin down to a fairly simple example.
The basic idea of the bug is that it appears $this is a different value in the constructor of a
class then it is outside of the constructor.
Take this example:
<?
global $GlobaBlow; //will hold the reference of $blow
class foobar {
var $State; //Holds the current state of an instanticated class
function foobar() {
$this->work(); //Sets GlobalBlow to the reference of the instanciated class
$this->State = "In Constructor"; //Sets the state of the instanciated class to
"In Constructor"
}
function work() {
$GLOBALS["GlobalBlow"] =& $this; //Assigns the Global variable the reference to this
instanciated class
}
}
$blow = new foobar(); //creates a new foobar
//$blow->work();
$blow->State = "Global"; //sets the state of the instanciated foobar to
"Global"
echo $GlobalBlow->State; //should display "Global" but instead displays "In
Constructor"
?>
Whatever $this was pointing to when it was assigned to the global variable is different then the
$this assigned to the global variable outside of the constructor. You can see this just by moving
the call to $blow->work from within the constructor to outside of the constructor.
If this is done then the correct value of "Global" is displayed.
Please don't hassle me on uselessness of the example. :) I know it's useless but where
we're using it in our project it is a must. Our project relies heavily on the ability to
assign $this to a variable from within constructors.
Thanks tons for your help,
Mike
--
Edit Bug report at: http://bugs.php.net/?id=7455&edit=1