Edit report at https://bugs.php.net/bug.php?id=49369&edit=1

 ID:                 49369
 Comment by:         le...@php.net
 Reported by:        admin at ifyouwantblood dot de
 Summary:            Change current(), key(), next(), etc. to check for
                     Iterator
 Status:             Open
 Type:               Feature/Change Request
 Package:            Arrays related
 Operating System:   *
 PHP Version:        *
 Block user comment: N
 Private report:     N

 New Comment:

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?


Previous Comments:
------------------------------------------------------------------------
[2012-07-01 11:14:30] 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. http://php.net/manual/en/language.oop5.iterations.php.

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

Expected result:
6

Actual result:
5

------------------------------------------------------------------------
[2009-08-26 10:34:05] admin at ifyouwantblood dot de

Description:
------------
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:
---------------
<?php

class iterator_array implements Iterator
{
        protected $aarray;
        
        public function __construct($array)
        {
                $this->aarray=$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()
        {
                next($this->aarray);
        }
                
        public function rewind()
        {
                reset($this->aarray);
        }
}

$i=new iterator_array(Array(1,2));
var_dump(current($i));
var_dump(key($i));
next($i);
var_dump($current($i));
var_dump(key($i));

Expected result:
----------------
int(1)
int(0)
int(2)
int(1)


Actual result:
--------------
array(6) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
  [5]=>
  int(6)
}
string(9) "&#65533;*&#65533;aarray"
bool(false)
NULL



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=49369&edit=1

Reply via email to