termn8r Posted October 22, 2007 Share Posted October 22, 2007 I have done queries like this for about 8 years. It may not be the best way but it's what 90% of my websites use... When I upgraded to 5.2.4 I realized it doesn't work. while($session->db->next_record()) { $menu->id = $session->db->f('sub_id'); $menu->name = stripslashes($session->db->f('sub_desc')); $menu->link = $session->db->f('link'); $menus[] = $menu; } Normally I can call this later with a simple statement. For simplicity lets just say for($m=0;$m<count($menus);$m++) { $menu = $menus[$m]; echo "<a href=$menu->link>$menu->name</a><BR>"; } Prior to new server this would list out all menus. Currently it lists (lets say 10 rows) of the last menu line. So instead of "Home,Login,Help" I get "Help,Help,Help" I am not looking for someone to say "Why don't you just query this way" because of the severity of this issue if I update 40+ websites to PHP 5.x. Is it purely 5.2 doing this? Or something else? Maybe I was using some bug all this time in the way Array[]'s are handled now or some kind of variable setting? Any help is appreciated. Quote Link to comment Share on other sites More sharing options...
trq Posted October 22, 2007 Share Posted October 22, 2007 I don't really see any issue with your code. Your using the stdClass object and storing multiple copies of it in an array, should work fine. What does.... <?php print_r($menus); ?> produce? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 22, 2007 Share Posted October 22, 2007 Shouldn't there be a $menu = new someclass; in there. Quote Link to comment Share on other sites More sharing options...
trq Posted October 22, 2007 Share Posted October 22, 2007 Nah, you can simply use the stdClass object. <?php $foo->bar = "bob"; print_r($foo); ?> Personally though, an assoc array is probably a little easier to follow. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 22, 2007 Share Posted October 22, 2007 then as he never creates a new instance of $menu, isn't always the same menu object each time through the loop? Quote Link to comment Share on other sites More sharing options...
trq Posted October 22, 2007 Share Posted October 22, 2007 No, because he is storing multiple instances of the stdClass object in an array and then looping through that. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 22, 2007 Share Posted October 22, 2007 I suspect it's due to PHP4 always creating copies of objects, whereas with PHP5, objects are always by reference, so each stdclass $menu in v5.0 points to the same object, unless instatiated as new one. Test script <?php class foo { var $bar; } $a = array (1,2,3); $b = $c = array(); foreach ($a as $v) { $foo->bar = $v; $b[] = $foo; } echo '<pre> B ', print_r($b, true), '</pre>'; foreach ($a as $v) { $foo = new foo; // this time, instantiate new object $foo->bar = $v; $c[] = $foo; } echo '<pre> C ', print_r($c, true), '</pre>'; ?> PHP 4 output (works for both arrays) [pre] B Array ( [0] => stdClass Object ( [bar] => 1 ) [1] => stdClass Object ( [bar] => 2 ) [2] => stdClass Object ( [bar] => 3 ) ) C Array ( [0] => foo Object ( [bar] => 1 ) [1] => foo Object ( [bar] => 2 ) [2] => foo Object ( [bar] => 3 ) ) [/pre] PHP 5 output (does not work unless use new) [pre] B Array ( [0] => stdClass Object ( [bar] => 3 ) [1] => stdClass Object ( [bar] => 3 ) [2] => stdClass Object ( [bar] => 3 ) ) C Array ( [0] => foo Object ( [bar] => 1 ) [1] => foo Object ( [bar] => 2 ) [2] => foo Object ( [bar] => 3 ) ) [/pre] Quote Link to comment Share on other sites More sharing options...
trq Posted October 22, 2007 Share Posted October 22, 2007 To be honest, ive never really tried to use the stdClass object for any sort of storage (after all, thats what associative arrays are for IMO), so ive never tried to loop through multiple instances either. After running some tests, it appears I have the same issue. #!/usr/bin/php <?php $a = range('a','c'); foreach ($a as $v) { $m->val = $v; $b[] = $m; print_r($b); print_r($m); } ?> produces.... Array ( [0] => stdClass Object ( [val] => a ) ) stdClass Object ( [val] => a ) Array ( [0] => stdClass Object ( [val] => b ) [1] => stdClass Object ( [val] => b ) ) stdClass Object ( [val] => b ) Array ( [0] => stdClass Object ( [val] => c ) [1] => stdClass Object ( [val] => c ) [2] => stdClass Object ( [val] => c ) ) stdClass Object ( [val] => c ) which (if anything) is just a bit wierd. I think it might have something to do with the way references changed in php5. Id'e hate to say it, but I think you may need to change from using the stdClass to associative arrays like the rest of us. Quote Link to comment Share on other sites More sharing options...
trq Posted October 22, 2007 Share Posted October 22, 2007 I suspect it's due to PHP4 always creating copies of objects, whereas with PHP5, objects are always by reference, so each stdclass $menu in v5.0 points to the same object, unless instatiated as new one. Yeah. just figured that out myself aswell. Quote Link to comment Share on other sites More sharing options...
termn8r Posted October 23, 2007 Author Share Posted October 23, 2007 Adding a simple "$menu = new menu" fixed it all. Thanks guys. It will take some work but I think I can manage to add this to all my pages. Solved as far as I'm concerned. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.