php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #6896 A class cannot have a reference to it's instance
Submitted: 2000-09-27 01:52 UTC Modified: 2000-12-06 16:56 UTC
From: davefox at angryrobot dot com Assigned:
Status: Closed Package: Class/Object related
PHP Version: 4.0.2 OS: RedHat Linux 6.2
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
6 + 48 = ?
Subscribe to this entry?

 
 [2000-09-27 01:52 UTC] davefox at angryrobot dot com
This bug appeared when I installed php4.0.2, I had 
php4.0.1RC before.

I'm trying to add a reference to Parent to each Child as its created. It seems like the Parent->baseObj property cannot be a reference to $this, its instance.

An example of the code that caused the problem:

<? 
class Parent { 
	function Parent(){ 
	$this->baseObj = &$this; 
	}
	
	function insert($name){ 
		/* This will work 
		if(get_class($this) == 'parent'){
			$this->test[$name] = new Child(); 				$this->test[$name]->baseObj = &$this; 
		} 
		else {
			$this->baseObj->test[$name] = new Child(); 
			$this->baseObj->test[$name]->baseObj = &$this->baseObj; 
		} 
		/* 
		/* This will not - says error: call to member function on non-object */ 
		$this->baseObj->test[$name] = new Child(); 			$this->baseObj->test[$name]->baseObj = &$this->baseObj; 
		} 
} 

class Child extends Parent{ 
	function Child(){ 
		$this->whatever = 'sometext'; 
	} 
	function showMe(){ 
		echo "I exist"; 
	} 
} 

$obj = new Parent();
$obj->insert('foo');
$obj->test['foo']->insert('bar');
$obj->test['bar']->insert('blah');
$obj->test['foo']->showMe();
$obj->test['bar']->showMe();
$obj->test['blah']->showMe(); 

?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-10-05 17:51 UTC] stas@php.net
Circular references are not currently supported
 [2000-12-06 16:56 UTC] waldschrott@php.net
fixed in cvs, it should work...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Feb 26 09:01:26 2024 UTC