#31942 [Opn->Fbk]: foreach and object members mixing badly
ID: 31942
Updated by: sniper@php.net
Reported By: adrian-phpbugs at sixfingeredman dot net
-Status: Open
+Status: Feedback
-Bug Type: Variables related
+Bug Type: Arrays related
Operating System: Linux
-PHP Version: PHP 4.3.11-dev
+PHP Version: 4.3.11-dev
New Comment:
Please try using this CVS snapshot:
http://snaps.php.net/php5-latest.tar.gz
For Windows:
http://snaps.php.net/win32/php5-win32-latest.zip
Previous Comments:
------------------------------------------------------------------------
[2005-02-12 00:22:04] adrian-phpbugs at sixfingeredman dot net
The version says: (built: Jan 5 2005 08:54:18)
------------------------------------------------------------------------
[2005-02-12 00:19:59] adrian-phpbugs at sixfingeredman dot net
Description:
------------
This might be related to #5052. The code below illustrates the problem
pretty well -- the class method should be returning a fresh copy of the
field but in fact the return value is sharing the foreach-counter so
that nested foreaches break. If I do something to force a copy, like
"$foo->_member =& $foo->_member" (I have no idea *why* this forces a
copy), then it works.
Reproduce code:
---------------
<?
class foo {
var $_member = array(1, 2, 3);
function member() {
return $this->_member;
}
}
$foo = new foo();
#$foo->_member =& $foo->_member;
$size = 0;
foreach ($foo->member() as $v) {
$size++;
foreach ($foo->member() as $v) {}
}
if ($size < count($foo->member())) die("BUG FOUND");
Expected result:
----------------
I expect $size == count($foo->member()). If you uncomment the commented
line, it works.
Actual result:
--------------
The inner foreach iterates through every value, but the outer foreach
only iterates through the first value.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=31942&edit=1
Thread (6 messages)