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.CachingIterator
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
CachingIterator
Референца за `class.cachingiterator.php` со подобрена типографија и навигација.
Класата CachingIterator
класата mysqli_driver
Вовед
Овој објект поддржува кеширана итерација над друг итератор.
Синопсис на класата
Претходно дефинирани константи
CachingIterator::CALL_TOSTRING-
Претвори го секој елемент во стринг.
CachingIterator::CATCH_GET_CHILD-
Не фрлај исклучок при пристап до деца.
CachingIterator::TOSTRING_USE_KEY-
од PHP 8.0.0. Силно се обесхрабрува потпирањето на оваа функција. key за конверзија во стринг.
CachingIterator::TOSTRING_USE_CURRENT-
од PHP 8.0.0. Силно се обесхрабрува потпирањето на оваа функција. current за конверзија во стринг.
CachingIterator::TOSTRING_USE_INNER-
од PHP 8.0.0. Силно се обесхрабрува потпирањето на оваа функција. inner за конверзија во стринг.
CachingIterator::FULL_CACHE-
Кеширај ги сите прочитани податоци.
Дневник на промени
| Верзија | = NULL |
|---|---|
| 8.0.0 | CachingIterator implements Serializable now. |
Содржина
- CachingIterator::__construct — Конструирај нов CachingIterator објект за итераторот
- CachingIterator::count — Бројот на елементи во итераторот
- CachingIterator::current — Враќање на тековниот елемент
- CachingIterator::getCache — Преземи ја содржината на кешот
- CachingIterator::getFlags — Земи ги користените знаменца
- CachingIterator::hasNext — Провери дали внатрешниот итератор има валиден следен елемент
- CachingIterator::key — Врати го клучот за тековниот елемент
- CachingIterator::next — Помести го итераторот напред
- CachingIterator::offsetExists — Целта offsetExists
- CachingIterator::offsetGet — Целта offsetGet
- CachingIterator::offsetSet — Целта offsetSet
- CachingIterator::offsetUnset — Целта offsetUnset
- CachingIterator::rewind — Назад итераторот
- CachingIterator::setFlags — Целта на setFlags
- CachingIterator::__toString — Врати ја стринг репрезентацијата на тековниот елемент
- CachingIterator::valid — Враќа текстуелна содржина
Белешки од корисници 4 белешки
<?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)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.