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
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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: youngm at effinitylabs dot com
New email:
PHP Version: OS:

 

 [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-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 22:01:26 2024 UTC