Having problems with storing a reference to the parent

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sairon
Forum Newbie
Posts: 4
Joined: Mon Dec 30, 2002 6:51 pm

Having problems with storing a reference to the parent

Post by Sairon »

I'm having problems with storing a reference to the parent in the child, here's an example demonstrating how I'm doing (I've tried a lot of other things as well thou)

Code: Select all

<?php

class C1
&#123;
	var $ref;
	function C1()
	&#123;
	&#125;
&#125;
class C2
&#123;
	var $var;
	var $child;
	function C2()
	&#123;
		$this->var = 'original';
		$this->child = new C1();
		$this->child->ref = &$this;
	&#125;
&#125;
$test = new C2();
$test->var = 'modified';
print $test->child->ref->var;
print '<br />';
print $test->var;

/*
this outputs:
original
modified

What I want is to store the reference of the parent in the child, is that possible in some way?
*/
?>
What I of course would have expected from this example is that it would output modified 2 times.

Grateful for any help :)
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Change
$test = new C2();
to
$test =& new C2();
Sairon
Forum Newbie
Posts: 4
Joined: Mon Dec 30, 2002 6:51 pm

Post by Sairon »

Thx a lot :)
Post Reply