I've designed an architecture in PHP which is dependent on a class hierarchy
which has a parent class with a member variable which is a child object.
That child object contains a reference back to the parent. The problem I'm
running into is changes to the parent's member variables are not reflected
in the child's reference to the parent. Let me use a code example to
illustrate my point.

Class Parent {
  var $sTest = "apple";
  var $pChild;
  function Parent() {
    $this->pChild = new Child();
    $this->pChild->SetParent($this);
  }
}

Class Child {
  var $pParent;

  function SetParent(&$pParent) {
    $this->pParent = &$pParent;
  }
  function PrintParentData() {
    echo "Parents Test data: ".$this->pParent->sTest."<Br>\n";
  }
}

$pParent = new Parent();
$pParent->sTest = "orange";
echo "pParent->sTest = ".$pParent->sTest."<br>\n";
$pParent->pChild->PrintParentData();

// Prints:
// pParent->sTest = orange
// Parents Test data: apple

// Instead of :
// pParent->sTest = orange
// Parents Test data: orange

I've also tried changing the first line of the Parent constructor to :
    $this->pChild = & new Child();


I think I've coded this correctly as per the docs on references. I'm using
PHP4.1.1. My conclusions are :

1. This is not possible in PHP due to safety mechanisms built into the
language to safeguard against circular references (and infinite recursion).
2. I'm doing something wrong.

Thanks to anyone willing to take a stab...

Kevin Morris



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to