Re: Is this a bug?!!! I cna't believe! Sorry, if im wrong...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I did a little experimenting, and it looks like foreach is misbehaving,
but may I just don't get it, anyway check this out
(my php version and output are below):

class A { var $name; function A($str) { $this->name = $str; } }

// does not work as expected.
$arr = array();
$a = &new A("qaz");
$arr[0] = &$a;      unset($a);
$a = &new A("wsx"); print_r($a);
$arr[1] = &$a;      print_r($arr[1]);
print_r($arr);
foreach($arr as $a) { echo "(".$a->name.")"; }
echo "\n"; //    ^-- I thought this might be the problem, hence:
foreach($arr as $b) { echo "(".$b->name.")"; }
echo "\n--\n";

// works as expected
$arrB = array();
$arrB[0] = &new A("qaz");
$arrB[1] = &new A("wsx");
print_r($arrB);
foreach($arrB as $c) { echo "(".$c->name.")"; }
echo "\n--\n";

// works as expected
$arrC = array();
$f = new A("qaz");
$arrC[0] = $f;
$f = new A("wsx");
$arrC[1] = $f;
print_r($arrC);
foreach($arrC as $d) { echo "(".$d->name.")"; }
echo "\n--\n";

// works as expected
$arrD = array();
$h =& new A("qaz");
$arrD[0] = $h;
$h =& new A("wsx");
$arrD[1] = $h;
print_r($arrD);
foreach($arrD as $g) { echo "(".$g->name.")"; }
echo "\n--\n";

// both items in the array reference the
// second object (references $i) - this is correct
$arrE = array();
$i = new A("qaz");
$arrE[0] =& $i;
$i = new A("wsx");
$arrE[1] =& $i;
print_r($arrE);
foreach($arrE as $j) { echo "(".$j->name.")"; }
echo "\n--\n";



---
which gives the following output on
PHP 5.0.2 (cli) (built: Nov  9 2004 19:00:36):


A Object ( [name] => wsx ) A Object ( [name] => wsx ) Array ( [0] => A Object ( [name] => qaz )

    [1] => A Object
        (
            [name] => wsx
        )

)
(qaz)(qaz)
(qaz)(qaz)
--
Array
(
    [0] => A Object
        (
            [name] => qaz
        )

    [1] => A Object
        (
            [name] => wsx
        )

)
(qaz)(wsx)
--
Array
(
    [0] => A Object
        (
            [name] => qaz
        )

    [1] => A Object
        (
            [name] => wsx
        )

)
(qaz)(wsx)
--
Array
(
    [0] => A Object
        (
            [name] => qaz
        )

    [1] => A Object
        (
            [name] => wsx
        )

)
(qaz)(wsx)
--
Array
(
    [0] => A Object
        (
            [name] => wsx
        )

    [1] => A Object
        (
            [name] => wsx
        )

)
(wsx)(wsx)
--


news.php.net wrote:
No


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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux