Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

EmptyIterator

Provide an Iterator with no positions when a routine expects an iterator but has nothing to yield. rewind and next leave it invalid, and foreach executes no iterations.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

new EmptyIterator()

Provide an Iterator with no positions when a routine expects an iterator but has nothing to yield. rewind and next leave it invalid, and foreach executes no iterations. Calling current or key directly on this empty implementation raises BadMethodCallException.

$it=new EmptyIterator(); $it->rewind(); $it->next();
try{$it->current();}catch(BadMethodCallException $e){$error=get_class($e);}
echo json_encode([$it->valid(),iterator_to_array($it),$error]);
[false,[],"BadMethodCallException"]

Examples

<?php
$it=new EmptyIterator(); $it->rewind(); $it->next();
try{$it->current();}catch(BadMethodCallException $e){$error=get_class($e);}
echo json_encode([$it->valid(),iterator_to_array($it),$error]);

Result

[false,[],"BadMethodCallException"]

Sources