The CachingIterator class

(PHP 5, PHP 7, PHP 8)

简介

This object supports cached iteration over another iterator.

类摘要

CachingIterator extends IteratorIterator implements OuterIterator , ArrayAccess , Countable {
/* 常量 */
const int CALL_TOSTRING = 1 ;
const int CATCH_GET_CHILD = 16 ;
const int TOSTRING_USE_KEY = 2 ;
const int TOSTRING_USE_CURRENT = 4 ;
const int TOSTRING_USE_INNER = 8 ;
const int FULL_CACHE = 256 ;
/* 方法 */
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

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

User Contributed Notes

xedin dot unknown at gmail dot com 07-May-2020 05:19
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.
ahmad dot mayahi at gmail dot com 25-Apr-2020 09:53
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.
jerome at chaman dot ca 15-Dec-2019 12:37
"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)
ahmad dot mayahi at gmail dot com 24-Oct-2016 07:54
<?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());
?>
PHP8中文手册 站长在线 整理 版权归PHP文档组所有