Reply to Re: [PHP] Is this a bug?!!! I cna't believe! Sorry, if im wrong...

Your name:

Reply:


Posted by Jochem Maas on 01/30/05 19:42

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
>>

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация