parent root
PHP: EmptyIterator - Manual

The EmptyIterator class

(PHP 5 >= 5.1.0, PHP 7)

Introduction

The EmptyIterator class for an empty iterator.

Class synopsis

EmptyIterator implements Iterator {
/* Methods */
public current ( void ) : mixed
public key ( void ) : scalar
public next ( void ) : void
public rewind ( void ) : void
public valid ( void ) : bool
}

Table of Contents

add a noteadd a note

User Contributed Notes 1 note

up
6
Ben
2 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
parent root