Laracon EU Online 2021

La clase CachingIterator

(PHP 5, PHP 7)

Introducción

Este objeto admite iteraciones almacenadas en caché sobre otros iteradores.

Sinopsis de la Clase

CachingIterator extends IteratorIterator implements OuterIterator , ArrayAccess , Countable {
/* Constantes */
const integer CALL_TOSTRING = 1 ;
const integer CATCH_GET_CHILD = 16 ;
const integer TOSTRING_USE_KEY = 2 ;
const integer TOSTRING_USE_CURRENT = 4 ;
const integer TOSTRING_USE_INNER = 8 ;
const integer FULL_CACHE = 256 ;
/* Métodos */
public __construct ( Iterator $iterator [, int $flags = self::CALL_TOSTRING ] )
public count ( void ) : int
public current ( void ) : void
public getCache ( void ) : array
public getFlags ( void ) : int
public getInnerIterator ( void ) : Iterator
public hasNext ( void ) : void
public key ( void ) : scalar
public next ( void ) : void
public offsetExists ( mixed $index ) : void
public offsetGet ( string $index ) : void
public offsetSet ( mixed $index , mixed $newval ) : void
public offsetUnset ( string $index ) : void
public rewind ( void ) : void
public setFlags ( int $flags ) : void
public __toString ( void ) : void
public valid ( void ) : void
}

Constantes predefinidas

CachingIterator::CALL_TOSTRING

Convierte cada elemento a un string.

CachingIterator::CATCH_GET_CHILD

No lanzar una excepción en el acceso a los hijos.

CachingIterator::TOSTRING_USE_KEY

Usa key para la conversión a string.

CachingIterator::TOSTRING_USE_CURRENT

Usa current para la conversión a string.

CachingIterator::TOSTRING_USE_INNER

Usa inner para la conversión a string.

CachingIterator::FULL_CACHE

Almacena en caché todos los datos leídos.

Tabla de contenidos

add a note add a note

User Contributed Notes 4 notes

up
8
ahmad dot mayahi at gmail dot com
4 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
ahmad dot mayahi at gmail dot com
6 months 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
0
xedin dot unknown at gmail dot com
6 months 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.
up
0
jerome at chaman dot ca
11 months 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)
To Top