PHP 8.4.1 Released!

The EmptyIterator class

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

简介

The EmptyIterator class for an empty iterator.

类摘要

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

目录

添加备注

用户贡献的备注 1 note

up
8
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