CKTIS / PHP
IteratorIterator
Adaptuje źródło Traversable do interfejsu kursora Iterator.
Wersja: PHP 8.4; repository runtime, production runtime not attested
Składnia
new IteratorIterator($traversable)Adaptuje źródło Traversable do interfejsu kursora Iterator. Przydaje się dla IteratorAggregate, gdy kod wymaga current, next oraz key. Przed ręcznym odczytem wykonaj rewind na opakowaniu. Dane nie są kopiowane z góry do tablicy.
class Source implements IteratorAggregate { public function getIterator(): Traversable {yield "x"=>4;yield "y"=>8;} }
$it=new IteratorIterator(new Source()); $it->rewind(); $first=$it->key(); $it->next(); echo json_encode([$first,$it->key(),$it->current()]);
["x","y",8]
Przykłady
<?php
class Source implements IteratorAggregate { public function getIterator(): Traversable {yield "x"=>4;yield "y"=>8;} }
$it=new IteratorIterator(new Source()); $it->rewind(); $first=$it->key(); $it->next(); echo json_encode([$first,$it->key(),$it->current()]);
Wynik
["x","y",8]