The CachingIterator class
(PHP 5)
Introduction
This object supports cached iteration over another iterator.
Class synopsis
/* Constants */
/* Methods */
}Predefined Constants
CachingIterator::CALL_TOSTRING
-
Convert every element to string.
CachingIterator::CATCH_GET_CHILD
-
Don't throw exception in accessing children.
CachingIterator::TOSTRING_USE_KEY
-
Use key for conversion to string.
CachingIterator::TOSTRING_USE_CURRENT
-
Use current for conversion to string.
CachingIterator::TOSTRING_USE_INNER
-
Use inner for conversion to string.
CachingIterator::FULL_CACHE
-
Cache all read data.
Table of Contents
- CachingIterator::__construct — Construct a new CachingIterator object for the iterator.
- CachingIterator::count — The number of elements in the iterator
- CachingIterator::current — Return the current element
- CachingIterator::getCache — Retrieve the contents of the cache
- CachingIterator::getFlags — Get flags used
- CachingIterator::getInnerIterator — Returns the inner iterator
- CachingIterator::hasNext — Check whether the inner iterator has a valid next element
- CachingIterator::key — Return the key for the current element
- CachingIterator::next — Move the iterator forward
- CachingIterator::offsetExists — The offsetExists purpose
- CachingIterator::offsetGet — The offsetGet purpose
- CachingIterator::offsetSet — The offsetSet purpose
- CachingIterator::offsetUnset — The offsetUnset purpose
- CachingIterator::rewind — Rewind the iterator
- CachingIterator::setFlags — The setFlags purpose
- CachingIterator::__toString — Return the string representation of the current element
- CachingIterator::valid — Check whether the current element is valid
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Другие базовые расширения
- Стандартная библиотека PHP (SPL)
- Класс AppendIterator
- Класс ArrayIterator
- Класс CachingIterator
- Класс CallbackFilterIterator
- Класс DirectoryIterator
- Класс EmptyIterator
- Класс FilesystemIterator
- Класс FilterIterator
- Класс GlobIterator
- Класс InfiniteIterator
- Класс IteratorIterator
- Класс LimitIterator
- Класс MultipleIterator
- Класс NoRewindIterator
- Класс ParentIterator
- Класс RecursiveArrayIterator
- Класс RecursiveCachingIterator
- Класс RecursiveCallbackFilterIterator
- Класс RecursiveDirectoryIterator
- Класс RecursiveFilterIterator
- Класс RecursiveIteratorIterator
- Класс RecursiveRegexIterator
- Класс RecursiveTreeIterator
- Класс RegexIterator
Коментарии
<?php
//This snippet will print out all the cached elements (foreach) .
$cache = new CachingIterator(new ArrayIterator(range(1,100)), CachingIterator::FULL_CACHE);
foreach ($cache as $c) {
}
print_r($cache->getCache());
?>
"cached iteration over another iterator" means this iterator is always one step behind the inner iterator. In other words, the "first" iteration will yield null:
<?php
$cit = new CachingIterator( new ArrayIterator( [ 'a', 'b', 'c'] ) );
echo $cit->current() ); // null
echo $cit->getInnerIterator()->current() ); // "a"
while($cit->hasNext()){
// we start with a "next" since the "first" item is null
$cit->next();
echo $cit->current(), '<br>';
}
?>
iterating this way gives us an access, ahead, to the future item (aka current item of the inner iterator)
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;
}
}
// C, C++, C#, PHP, Python, Go, and Ruby
?>
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.
Apparently, the `FULL_CACHE` flag automatically cancels the default flag `CALL_TOSTRING`. This is evident when one of the values cannot be converted to string: with the default `CALL_TOSTRING` flag, it would throw an error; without that flag, or with the `FULL_CACHE` flag, it does not.