Edit report at http://bugs.php.net/bug.php?id=52560&edit=1
ID: 52560
Comment by: tobias382 at gmail dot com
Reported by: tobias382 at gmail dot com
Summary: FilterIterator errantly returns null for first
element
Status: Open
Type: Bug
Package: SPL related
Operating System: Ubuntu 10.04
PHP Version: 5.3SVN-2010-08-07 (snap)
Block user comment: N
New Comment:
Small amendment: $iterator->rewind() should be $this->rewind() in the
overridden
constructor in the test script.
Previous Comments:
------------------------------------------------------------------------
[2010-08-07 04:40:26] tobias382 at gmail dot com
Description:
------------
Even when a FilterIterator subclass accepts all elements, it returns
NULL for the
first element in the original array. There are a few methods to correct
this
behavior, noted in the attached test script.
Test script:
---------------
<?php
class FooIterator extends FilterIterator {
/*
// Forces this class to produce expected behavior below
// (i.e. 1, 2, NULL instead of NULL, 2, NULL)
public function __construct(Iterator $iterator) {
parent::__construct($iterator);
$iterator->rewind();
}
*/
// Should cause all elements to be accepted
public function accept() {
return true;
}
/*
// Another way to force this class to produce expected behavior
below
public function current() {
return $this->getInnerIterator()->current();
}
*/
}
$array = array(1, 2);
/*
// What should happen in subsequent blocks
$iterator1 = new ArrayIterator($array);
var_dump($iterator1->current()); // int(1)
$iterator1->next();
var_dump($iterator1->current()); // int(2)
$iterator1->next();
var_dump($iterator1->current()); // NULL
*/
// The problem
$iterator2 = new FooIterator(new ArrayIterator($array));
var_dump($iterator2->current()); // NULL <-- this should be int(1)
$iterator2->next();
var_dump($iterator2->current()); // int(2)
$iterator2->next();
var_dump($iterator2->current()); // NULL
// Exhibits correct behavior with no changes to FooIterator
$iterator3 = new FooIterator(new ArrayIterator($array));
$iterator3->rewind(); // This line shouldn't be needed, but is to force
expected behavior
var_dump($iterator3->current()); // int(1)
$iterator3->next();
var_dump($iterator3->current()); // int(2)
$iterator3->next();
var_dump($iterator3->current()); // NULL
Expected result:
----------------
int(1)
int(2)
NULL
int(1)
int(2)
NULL
Actual result:
--------------
NULL
int(2)
NULL
int(1)
int(2)
NULL
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=52560&edit=1