|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31942 foreach and object members mixing badly
Submitted: 2005-02-12 00:19 UTC Modified: 2005-09-10 01:00 UTC
Avg. Score:4.0 ± 0.8
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:3 (100.0%)
From: adrian-phpbugs at sixfingeredman dot net Assigned:
Status: No Feedback Package: Arrays related
PHP Version: 4.3.11-dev OS: Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2005-02-12 00:19 UTC] adrian-phpbugs at sixfingeredman dot net
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) {
        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.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-12 00:22 UTC] adrian-phpbugs at sixfingeredman dot net
The version says: (built: Jan  5 2005 08:54:18)
 [2005-09-02 07:46 UTC]
Please try using this CVS snapshot:
For Windows:

 [2005-09-10 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2006-04-01 13:03 UTC] kevin at planetsaphire dot com
My intranet project appears to be having problems due to this bug.  I am running PHP Version 5.1.2.  I noticed that this bug was originally filed against PHP 4.3.11, and can file a seperate report if I need to.

The phpinfo file on my server:

On wwwbiz, if I have an inventory category three leaves or more deep, it will not be matched to a higher leaf, which causes the script to call my error_dbdown() function.  This is being caused by the foreach() syntax, and it happens regardless if I use the reference operator or not.

Sources can be viewed from:
cvs co -P wwwbiz

File possibly affected by this bug is: db/mysql-inv.php
 [2006-04-02 23:37 UTC] kevin at planetsaphire dot com
My project was missing the following code after calling a recursive function.  As a result, my project does not re-produce the bug described in this report:

/* Let's sort through the subcategory's arrays. */
$retval = 
$this->getProperItem($priv_item, $relationNum);
if ( $retval != NULL )
	return $retval;
 [2008-03-19 17:29 UTC] php-bugs dot trippyd at spamgourmet dot com
following up on this bug, we have test code similar to what was shown, the problem still exists in both 4.4.7 and 4.4.8.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 15 12:01:29 2024 UTC