|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #49369 Change current(), key(), next(), etc. to check for Iterator
Submitted: 2009-08-26 10:34 UTC Modified: 2020-06-14 16:37 UTC
Avg. Score:3.8 ± 1.1
Reproduced:21 of 24 (87.5%)
Same Version:10 (47.6%)
Same OS:11 (52.4%)
From: admin at ifyouwantblood dot de Assigned:
Status: Suspended Package: Arrays related
PHP Version: * OS: *
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2009-08-26 10:34 UTC] admin at ifyouwantblood dot de
it would be helpful for chained Iterators, if the default array functions would check if an given object is an instanceof Iterator and react appropriate.

thus key() calling object->key(), current() calling object->current() and so on.

Reproduce code:

class iterator_array implements Iterator
	protected $aarray;
	public function __construct($array)
	public function key()
		return key($this->aarray);
	public function current()
		return current($this->aarray);
	public function valid()
		return (current($this->aarray)!==FALSE);
	public function next()
	public function rewind()

$i=new iterator_array(Array(1,2));

Expected result:

Actual result:
array(6) {
string(9) "&#65533;*&#65533;aarray"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-01 15:47 UTC]
-Summary: make current(),key(),next() etc. check for Iterator +Summary: Change current(), key(), next(), etc. to check for Iterator -Package: Feature/Change Request +Package: Arrays related -Operating System: +Operating System: * -PHP Version: 5.3.0 +PHP Version: *
 [2012-07-01 11:14 UTC] bugs dot php dot net dot nsp at cvogt dot org
As a further note, the current behavior or current() also leaks private fields unlike e.g.

class Test{
  private $field = 5;
  public $field3 = 6;
$t = new Test;
print current($t);

Expected result:

Actual result:
 [2012-11-16 04:27 UTC]
I'm not sure this is a php bug.  Iterators should be rewound almost 100% of the 
time before being used, especially when using an array or an object that 
implements Iterator instead of IteratorAggregate.

Maybe the documentation should try to make this better known?
 [2012-11-16 04:33 UTC]
Also, calling `current($i)` gets the current value of the object `$i` which is the 
array; it doesn't call `$i->current`.
 [2012-11-16 04:35 UTC]
Sorry, I misunderstood the bug. Silly me.  Ignore my above comments.
 [2012-12-09 01:48 UTC]
Added bug #63678 as a duplicate.
 [2016-12-09 12:32 UTC] tom at r dot je
Now that in 7.1 we can type hint iterable this would be very useful.

function foo(iterable $i) {
   $first = current($i);
   $last = end($i);


Because $i is an iterator or an array, we can't differentiate between $i->current() and current($i) we'd need to do:

function foo(iterable $i) {
 	if (is_array($i)) {
 		$first = current($i);
 		$last = end($i); 		
 	else if ($i instanceof Traversable) {
 		$first = $i->current();
 		foreach ($i as $x) {
 			$last = $x;

It would be a lot simpler if this logic was abstracted into the various functions so they can work with either Traversable or array types.
 [2020-06-14 16:37 UTC]
-Status: Open +Status: Suspended
 [2020-06-14 16:37 UTC]
This feature request would require the deprecation of these
functions working on the mangled property table of objects[1], and
eventually removing support for that.

Only after sufficient time has passed after this removal, changing
the functionality could even be considered.  And still,
introducing this feature would require the RFC process[2].  For
the time being, I'm suspending this ticket.

[1] <>
[2] <>
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Wed Sep 27 06:01:24 2023 UTC