CachingIterator クラス

(PHP 5, PHP 7, PHP 8)

はじめに

このオブジェクトは、別のイテレータからキャッシュされた反復処理をサポートします。

クラス概要

class CachingIterator extends IteratorIterator implements ArrayAccess, Countable, Stringable {
/* 定数 */
public const int CALL_TOSTRING;
public const int CATCH_GET_CHILD;
public const int TOSTRING_USE_KEY;
public const int TOSTRING_USE_INNER;
public const int FULL_CACHE;
/* メソッド */
public __construct(Iterator $iterator, int $flags = CachingIterator::CALL_TOSTRING)
public count(): int
public current(): mixed
public getCache(): array
public getFlags(): int
public hasNext(): bool
public key(): scalar
public next(): void
public offsetExists(string $key): bool
public offsetGet(string $key): mixed
public offsetSet(string $key, mixed $value): void
public offsetUnset(string $key): void
public rewind(): void
public setFlags(int $flags): void
public __toString(): string
public valid(): bool
/* 継承したメソッド */
}

定義済み定数

CachingIterator::CALL_TOSTRING

それぞれの要素を文字列に変換します

CachingIterator::CATCH_GET_CHILD

子要素にアクセスする際、例外をスローしません

CachingIterator::TOSTRING_USE_KEY

文字列に変換する際に key を使います。

CachingIterator::TOSTRING_USE_CURRENT

文字列に変換する際に current を使います。

CachingIterator::TOSTRING_USE_INNER

文字列に変換する際に inner を使います。

CachingIterator::FULL_CACHE

読み込んだデータを全てキャッシュします。

変更履歴

バージョン 説明
8.0.0 CachingIterator は、 Stringable を新たに実装しました。

目次

add a note add a note

User Contributed Notes 4 notes

up
10
ahmad dot mayahi at gmail dot com
4 years ago
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.
up
9
ahmad dot mayahi at gmail dot com
7 years ago
<?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());
?>
up
1
jerome at chaman dot ca
4 years ago
"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)
up
0
xedin dot unknown at gmail dot com
4 years ago
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.
To Top