Since i see from your code, you are trying to create an object which maintains a list (collection) of instances of another object. Is that correct? Or you are trying to inherit a class from a parent class? Then it's a little bit different story. If this is the case, just tell me and I'll provide you with another example.


The confusion comes from using 'parent' and 'child' as names for the class variables. I'll give you an example for the first case just keeping your name convention, but in general it's a good idea to use some more natural (descriptive) names for your variables, and especially to avoid 'parent/child' convention if the classes are not really inherited classes.

So, here it is the example, feel free to ask in case there is something unclear in it:

--------------------
<?php

  // class Object_1 defines the parent object
  class Object_1 {

var $_children;

    function Object_1() {
      $this->_children = array();
    }

    function addChild($child) {
      $this->_children[] = $child;
    }

    function getChildren() {
      return $this->_children;
    }
  }

  // class Object_2 defines the child object(s)
  class Object_2 {

var $_myName;

    function Object_2($name) {
      $this->_myName = $name;
    }

    function getName() {
      return $this->_myName;
    }
  }


// implementation, the long way


$MyParentObject = new Object_1;

  $MyChildObject_1 = new Object_2("child object 1");
  $MyChildObject_2 = new Object_2("child object 2");
  $MyChildObject_3 = new Object_2("child object 3");

  $MyParentObject->addChild($MyChildObject_1);
  $MyParentObject->addChild($MyChildObject_2);
  $MyParentObject->addChild($MyChildObject_3);

// implementation, the short way

  $MyParentObject->addChild(new Object_2("child object 4"));
  $MyParentObject->addChild(new Object_2("child object 5"));

// list all children registered in the parent object

  foreach ($MyParentObject->getChildren() as $achild) {
    echo $achild->getName() . "<br>";
  }

?>
--------------------

Hope this will help,

Boyan




Gareth Williams wrote:
Hi there,

I'm having trouble with passing objects as references. What I want to do is something like this:

class object_1
{
    var $my_chld;

var $my_array;

    function object_1()
    {
        $this->my_array = array('id' => 0, 'name'=>'');
        $this->my_child = new object_2($this);
    }

}

class object_2
{
    var $my_parent;
    function object_2(&$parent_object)
    {
        $this->my_parent = $parent_object;
    }

    function set_parent()
    {
        $this->my_parent->my_array['id'] = 1;
        $this-> my_parent->my_array['name'] = 'test';
    }
}

$instance = new object_1();
$instance->my_child->set_parent();

echo "instance: ".$instance->my_array['name']."<br>";
echo "parent: ".$instance->my_child->my_parent->my_array['name']."<br>";


The above code give the output:


instance:
parent: test

where I want it to give:

instance: test
parent: test

but somewhere along the way, the reference to the parent object is being broken.

I've looked all over the place, and I can't find the answer to this. Can somebody please help me?

Cheers,

Gareth


-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to