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 this is not your bug, you can add a comment by following this link.
If this is your bug, but 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

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: Sun Mar 03 10:01:29 2024 UTC