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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: davefox at angryrobot dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

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: Sun Dec 22 01:01:30 2024 UTC