php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49279 Current does not seem to work with Iterator
Submitted: 2009-08-17 13:45 UTC Modified: 2009-08-17 14:02 UTC
From: andrew at ajohnstone dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.2.10 OS: debain
Private report: No CVE-ID: None
 [2009-08-17 13:45 UTC] andrew at ajohnstone dot com
Description:
------------
Current does not seem to work with Iterator

Reproduce code:
---------------
<?php

class test implements IteratorAggregate, Iterator {

        protected $_data = array(
                0=>1,
                1=>2,
                2=>3,
                3=>4,
                4=>5,
        );

        public function getIterator() {
                var_dump(__FUNCTION__);
                return $this->getCurrentItems();
        }

        public function getCurrentItems() {
                return $this->_data;
        }

        public function current() {
                var_dump(__FUNCTION__);
                $this->_data = $this->getCurrentItems();
                return current($this->_data);
        }

        public function key() {
                var_dump(__FUNCTION__);
                return key($this->_data);
        }

        public function next() {
                var_dump(__FUNCTION__);
                next($this->_data);
        }

        public function rewind() {
                var_dump(__FUNCTION__);
                $this->_data = $this->getCurrentItems();
                $this->_pointer = 0;
                reset($this->_data);
        }

        public function valid() {
                var_dump(__FUNCTION__);
                return (current($this->_data) !== FALSE);
        }


}

$c = new test();
var_dump(current($c));


Expected result:
----------------
string 'current' (length=7)
1

Actual result:
--------------
bool(false)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-08-17 14:02 UTC] scottmac@php.net
None of the array functions work with iterator interface, its designed to allow you iterate through objects with foreach() / while().
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 27 04:00:03 2025 UTC