// create data object from the A $A_obj = new A(); // print the info property echo 'A_obj info: ' . $A_obj->info . '<br/>';
// create the B object and pass the A_obj we created above $B_obj = new B_class($A_obj); // print the info property through the B object to make sure it has the same value 'eeee' echo 'B_obj info: ' . $B_obj->A_obj->info . '<br/>';
// chage the info property $B_obj->change('xxxxx'); // print the info property through the B object to make sure it changed the value to 'xxxxxx' echo 'B_obj info after change: ' . $B_obj->A_obj->info . '<br/>'; // print the info property from the A_obj to see if the change through B_obj has affected it echo 'A_obj info: ' . $A_obj->info . '<br/>';
?>
The result:
A_obj info: eeee
B_obj info: eeee
B_obj info after change: xxxxx
A_obj info: xxxxx
strata_ranger at hotmail dot com (01-Oct-2009 02:59)
Note that in PHP5 you generally don't need the reference operator -- at all -- when dealing with class objects, because PHP5 implements objects using Instances (which are more like C pointers than PHP's references system).
For example: <?php //
// Since PHP 5
//
$foo = new stdClass();
// $bar and $foo are still holding the same (single) object $bar = $foo; var_dump($foo, $bar) // object(stdClass)#1 (0) { }
// object(stdClass)#1 (0) { }