Page 1 of 1

Having problems with storing a reference to the parent

Posted: Tue Jan 20, 2004 1:06 pm
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 :)

Posted: Tue Jan 20, 2004 1:46 pm
by markl999
Change
$test = new C2();
to
$test =& new C2();

Posted: Tue Jan 20, 2004 2:05 pm
by Sairon
Thx a lot :)