The only difference between CachingIterator and other Iterators such as ArrayIterator is the hasNext() method.
Since the data will be loaded into the memory, the CachingIterator is able to check whether the given iterator has a next element.
Let's demonstrate this by an example:
<?php
$iterator = new CachingIterator(new ArrayIterator(['C', 'C++', 'C#', 'PHP', 'Python', 'Go', 'Ruby']));
foreach ($iterator as $item) {
if ($iterator->hasNext()) {
echo $item.', ';
} else {
echo 'and '.$item;
}
}
?>
In this example I check whether the iterator has a next value, if so, I append a comma otherwise "and" will be appended to the last element.