Re: [PHP] Object References Problem

From: Boyan Nedkov (k24d88_at_zonnet.nl)
Date: 10/31/03


Date: Fri, 31 Oct 2003 00:41:38 +0100
To:  php-general@lists.php.net


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
>



Relevant Pages

  • finding class parent information
    ... I can get all of the compponets in a class, and get references to ... JLabel called myLabel belongs to parent JPanel called myPanel ... JButton called myButton belongs to parent JPanel called myPanel ...
    (comp.lang.java.programmer)
  • clutching on to my final attributes...
    ... When I deserialize my Parent object my 2 references to my 'A' object will ... private final Child c; ... void update{ ...
    (comp.lang.java.programmer)
  • Re: Duplicate server entries for the same site code
    ... I think so - if the child site is no longer referenced at the parent. ... >> run PREINST /DELSITE to clean up references to the site if any>> ... >>> Neither log on the parent server reports any entries for either server. ...
    (microsoft.public.sms.setup)
  • Re: TMJ re-revisited
    ... (Newsgroup threading is done based on the References: and Message-ID: ... change off as a new thread, but with a link to the parent post in the ...
    (rec.games.roguelike.angband)
  • Re: Fetching a thread
    ... [about a newsreader finding the parent to the current ... "References" line, but is that really what your reader does? ... out which reference is the Message-ID of the parent, ... This even works (if my server has the ...
    (alt.usage.english)