[PHP] problem with embeded objects and reference

2004-06-30 Thread Vincent DUPONT
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



RE: [PHP] problem with embeded objects and reference

2004-06-30 Thread Chris W. Parker
Vincent DUPONT mailto:[EMAIL PROTECTED]
on Wednesday, June 30, 2004 1:05 PM said:

 Hi,

hi.

 could anyone help on this 'by reference' problem.

when starting a new thread please do not just reply to an ongoing thread
and change the subject. please start a new email.


your fellow list members thank you.

chris.

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



Re: [PHP] problem with embeded objects and reference

2004-06-30 Thread Red Wingate
so, why don't you reply correctly? :p
Chris W. Parker wrote:
Vincent DUPONT mailto:[EMAIL PROTECTED]
on Wednesday, June 30, 2004 1:05 PM said:

Hi,

hi.

could anyone help on this 'by reference' problem.

when starting a new thread please do not just reply to an ongoing thread
and change the subject. please start a new email.
your fellow list members thank you.
chris.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] problem with embeded objects and reference

2004-06-30 Thread Red Wingate
better don't blame the other pal :-)
http://screens.erisx.de/reply.gif
  -- red
Red Wingate wrote:
so, why don't you reply correctly? :p
Chris W. Parker wrote:
Vincent DUPONT mailto:[EMAIL PROTECTED]
on Wednesday, June 30, 2004 1:05 PM said:

Hi,

hi.

could anyone help on this 'by reference' problem.

when starting a new thread please do not just reply to an ongoing thread
and change the subject. please start a new email.
your fellow list members thank you.
chris.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php