php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #7455 Problems with $this in constructor
Submitted: 2000-10-25 13:09 UTC Modified: 2001-03-29 11:41 UTC
From: youngm at effinitylabs dot com Assigned:
Status: Closed Package: *General Issues
PHP Version: 4.0.3 OS: Redhat 7.0
Private report: No CVE-ID: None
 [2000-10-25 13:09 UTC] youngm at effinitylabs dot com
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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-10-28 08:15 UTC] youngm at effinitylabs dot com
duplicate of bug#7482, and #7454

 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Tue Jul 16 11:01:25 2019 UTC