php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #34445 ArrayAccess implementation class cannot be "foreach"-ed
Submitted: 2005-09-09 20:30 UTC Modified: 2005-09-10 23:23 UTC
Votes:8
Avg. Score:4.0 ± 0.9
Reproduced:7 of 7 (100.0%)
Same Version:2 (28.6%)
Same OS:4 (57.1%)
From: stochnagara at hotmail dot com Assigned:
Status: Wont fix Package: Class/Object related
PHP Version: 5CVS-2005-09-09 (snap) OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2005-09-09 20:30 UTC] stochnagara at hotmail dot com
Description:
------------
I made a class which implements ArrayAccess. Then I try to use foreach on one of its array values and I get error "Objects used as arrays in post/pre increment/decrement must return values by reference"

I've reduced the big code fragment to this small one:

Reproduce code:
---------------
<?
class foo implements ArrayAccess {
	protected $params;			
	public function offsetGet ($field) { return $this->params[$field]; }
	public function offsetExists ($field) { return isset ($this->params[$field]); }	
	public function offsetUnset ($field) { unset ($this->params[$field]); }
	public function offsetSet ($field, $value) { $this->params[$field] = $value; }	
}

$foo = new foo;
foreach ($foo['options'] as $optionvalue => $optiontext) {}


Expected result:
----------------
no errors

Actual result:
--------------
Fatal error: Objects used as arrays in post/pre increment/decrement must return values by reference in ... on line 161

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-09-09 20:34 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

ArrayAccess is no Iterator 
 [2005-09-09 21:14 UTC] stochnagara at hotmail dot com
I do not agree with this explanation. I do not perform

foreach ($this as $k => $v) but foreach ($this['options'] as $k => $v)

Also i've tried with $this->params['options'] really set as an array and it does not work too.

My current workaround is 

$opt = $this['options'];
foreach ($opt as $k => $v)

I still consider this is a bug and if there are any technical difficulties for fixing it, I prefer to see bug status "Won't fix" rather then "Bogus".
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC