Jump to content

[SOLVED] Array issue since moving to 5.2.4 from 4.4.0


termn8r

Recommended Posts

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.

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]

 

 

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.