The EmptyIterator class

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

Introduction

The EmptyIterator class for an empty iterator.

Class synopsis

class EmptyIterator implements Iterator {
/* Methods */
public current(): never
public key(): never
public next(): void
public rewind(): void
public valid(): false
}

Table of Contents

add a note

User Contributed Notes 1 note

up
12
Ben
7 years ago
Example use case:

<?php
class MyIterator implements IteratorAggregate
{
/**
* @var string
*/
private $url;

/**
* MyIterator constructor.
* @param $url
*/
public function __construct($url)
{
$this->url = $url;
}

/**
* @inheritDoc
*/
public function getIterator()
{
$content = file_get_contents($this->url);
try {
return @new
SimpleXMLIterator($content);

} catch (
Exception $e) { / Case $content is not valid XML, but you don't care
return new EmptyIterator();
}
}

}
?>
To Top

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant