The Iterator interfaceIterator
&reftitle.intro;
Interface for external iterators or objects that can be iterated
themselves internally.
&reftitle.interfacesynopsis;
IteratorextendsTraversable&Methods;Predefined iterators
PHP already provides a number of iterators for many day to day tasks.
See SPL iterators for a list.
&reftitle.examples;
Basic usage
This example demonstrates in which order methods are called when
using &foreach; with an iterator.
position = 0;
}
public function rewind(): void {
var_dump(__METHOD__);
$this->position = 0;
}
#[\ReturnTypeWillChange]
public function current() {
var_dump(__METHOD__);
return $this->array[$this->position];
}
#[\ReturnTypeWillChange]
public function key() {
var_dump(__METHOD__);
return $this->position;
}
public function next(): void {
var_dump(__METHOD__);
++$this->position;
}
public function valid(): bool {
var_dump(__METHOD__);
return isset($this->array[$this->position]);
}
}
$it = new myIterator;
foreach($it as $key => $value) {
var_dump($key, $value);
echo "\n";
}
?>
]]>
&example.outputs.similar;
&reftitle.seealso;
See also object iteration.
&language.predefined.iterator.current;
&language.predefined.iterator.key;
&language.predefined.iterator.next;
&language.predefined.iterator.rewind;
&language.predefined.iterator.valid;