Re: problem with embeded objects and reference

From: Date: Wed, 30 Jun 2004 20:23:07 +0000
Subject: Re: problem with embeded objects and reference
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-189356@lists.php.net to get a copy of this message
Lill example: [...] $child1->setproperty("first");//displayed $main->setChild($child1); $child1->setproperty("second"); //NOT DISPLAYED [...] $child1 is passed to $main with property 'first'. Later on you update $child1, but not $main->child, as they are not the same objects ( equal but not same ). So i guess you need to -$main->setChild($child1); +$main->setChild(&$child1); and -$this->child = $child; +$this->child =& $child; // MUST !!! Now i see, $this->child = $child; copys the object instead of creating a ref. -- red Red Wingate wrote:
Not sure over here, but isn't it $main->setChild(&$child1); Vincent Dupont wrote:
Hi, could anyone help on this 'by reference' problem. I have 2 classes. The main class as a child class. The child class has properties (array) I would like to be able to manipulate the child's properties even after the child has been inserted into the main class. Does this make sense? I can do it with by getting a reference of the child class, then setting the property,like :
    $tmp = & $main->getChild(); //get a reference to the child object
    $tmp->setProperty("forth");//ok
OR
    $main->setChildProperty("third"); //ok
but Whyt can't I do it directly by calling a reference to the child, like in : $child1->setproperty("second"); //The property is not set or not displayed I know I need a reference to the child class that is within the maoin class, but HOW?? Here is a complete example <?php class class1{ var $child; //array
    function class1(){
        $this->properties = array();
    }
        //set the child object (class2)
    function & setChild($child){
        $this->child=$child;
    }
        //get the child object (class2), and call its setProperty method
    function setChildProperty($prop){         $theChild = & $this->child;
        $theChild->setProperty($prop);
    }
        //return a reference to the child object
    function & getChild(){
        return $this->child;
    }
        //print the child object properties
    function display(){
        $this->child->toString();
    }
} class class2{ var $properties; //array
        function class2(){
        $this->properties = array();
    }
        function & setProperty($new_property){
        $this->properties[] = $new_property;
    }
        function & getProperty($index){
        return $this->properties[$index];
    }
        function toString(){
        print_r($this->properties);
    }
} $main = & new class1(); $child1 = & new class2(); $child1->setproperty("first");//displayed $main->setChild($child1); $child1->setproperty("second"); //NOT DISPLAYED $main->setChildProperty("third"); //displayed $tmp = & $main->getChild(); //get a reference to the child object $tmp->setProperty("forth");//displayed $main->display(); //output : Array ( [0] => first [1] => third [2] => forth ) ?>


« previous php.general (#189356) next »