Re: Passing Object Refs throught Arrays
| From: | Tom Rogers | Date: | Thu, 30 Jan 2003 15:31:54 +0000 |
| Subject: | Re: Passing Object Refs throught Arrays | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-133656@lists.php.net to get a copy of this message | ||
Hi,
Thursday, January 30, 2003, 10:36:20 PM, you wrote:
J> I'm having a problem, and I wrote this sample code to illistrate it.
J> In this code, I'm trying to create a object, put it throught a que. I
J> then want something to happen to this object, and this change to be
J> reflected in the original object. I am aware that PHP uses copy
J> symantecs not refrence ones, so I have used "&" at all the places I can
J> see to use them. However it doesnt work: the output from this is still
J> 0 not 100 as it should be.
J> Does anyone have any suggestions, or URL's to really good guides on
J> reference passing?
J> Thanks in Advance,
J> James.
J> ---------------------
J> class test {
J> var $flag = 0;
J> }
J> // Make Object
J> $root = new test();
J> // Make Que
J> $que = array();
J> // Stick object on que
J> array_push($que, &$root);
J> // Take object from Que
J> $consider =& array_shift($que);
J> // Change something on the object we pulled from the que
$consider->>flag = 100;
J> // Test and see if it changed on the original object
echo($root->>flag);
Here are some replacements that I use for handling references as the normal pop
and push wont work:
function array_ref_push(&$array,&$ref){
$array[] =& $ref;
}
function &array_ref_pop(&$array){
$r =& $array[count($array)-1];
array_pop($array);
return $r;
}
function array_ref_unshift(&$array,&$ref){
array_unshift($array,'');
$array[0] =& $ref;
}
function &array_ref_shift(&$array){
$r =& $array[0];
array_shift($array);
return $r;
}
--
regards,
Tom