Iterator does not support rewinding

Description

Iterators are executed one way, and cannot be rewind: once they have reached the end of their list of value, they stay there. Some Iterator subclasses are able to rewind at the beginning of the list, though.

Example

<?php

$XML = <<< XML
<root>
  <item>1</item>
  <item>2</item>
  <item>3</item>
</root>
XML;

$dom = new DomDocument();
$dom->loadXml($XML);
$items = $dom->getElementsByTagName('item');

echo 'Count: '.count($items).PHP_EOL;
echo 'Count: '.iterator_count($items->getIterator()).PHP_EOL;
$it = new IteratorIterator($items);
echo 'Count: '.iterator_count($it).PHP_EOL;
echo 'Count: '.iterator_count($it).PHP_EOL;

?>

Solutions

  • Create a second iterator, based on the first one, and run the second operation with it.

  • Replace the usage of the Iterator by a subclass, that accepts rewind, such as ArrayIterator.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See ` <https://php-changed-behaviors.readthedocs.io/en/latest/behavior/.html>`_.