php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70978 reset(), current() result for objects implementing Iterator is bogus
Submitted: 2015-11-26 15:01 UTC Modified: 2015-11-26 15:14 UTC
From: danack@php.net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.0.0RC8 OS: N/A
Private report: No CVE-ID: None
 [2015-11-26 15:01 UTC] danack@php.net
Description:
------------
When calling reset() or current() on an object implementing an \Iterator, the return value of these functions is bogus.

Those functions are each meant to return the value, with current() returning the current value, and reset returning the value of the current element aka the first element.

Instead, for objects implementing \Iterator, the value returned is that of the current key.

Test script:
---------------
<?php

class myIterator implements Iterator 
{
    private $position = 0;
    private $array = array(
        "firstelement_Iterator",
        "secondelement_Iterator",
        "lastelement_Iterator",
    );  

    public function __construct() {
        $this->position = 0;
    }

    function rewind() {
        $this->position = 0;
    }

    function current() {
        return $this->array[$this->position];
    }

    function key() {
        return $this->position;
    }

    function next() {
        ++$this->position;
    }

    function valid() {
        return isset($this->array[$this->position]);
    }
}



$tests = [];

$test0 = [
    "firstelement_Array",
    "secondelement_Array",
    "lastelement_Array",
];


$test1 = new ArrayIterator([
    "firstelement_ArrayIterator",
    "secondelement_ArrayIterator",
    "lastelement_ArrayIterator",
]);

$test2 = new myIterator();

$tests = [
    'array' => $test0,
    'array_iterator' => $test1,
    'Iterator' => $test2,
];

foreach ($tests as $testName => $test) {
    echo "start test $testName\n";

    $current = current($test);
    $i = 0;

    $current = current($test);
    foreach ($test as $key => $value) {
        //echo "$key, $value \n";
    }

    //current returns the value of the array element that's currently being pointed to by the
    // internal pointer.
    // If the internal pointer points beyond the end of the elements list or the array is empty, current() returns FALSE.
    var_dump(current($test));
    // reset() rewinds array's internal pointer to the first element and
    // returns the value of the first array element - this should be firstelement_*
    var_dump(reset($test));
    
    echo "End test.\n\n";
}






Expected result:
----------------
start test array_iterator
string(26) "firstelement_Iterator"
string(26) "firstelement_Iterator"
End test.

Actual result:
--------------
start test Iterator
int(3)
int(3)
End test.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-11-26 15:14 UTC] danack@php.net
-Status: Open +Status: Not a bug
 [2015-11-26 15:14 UTC] danack@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

Apparently current() and reset() are not aware of \Iterator. And so the behaviour below is acceptable, if non-intuitive.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 09:00:02 2026 UTC