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 ) 
?>

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

Reply via email to