PHP 7.2.0 Release Candidate 2 Released

The EmptyIterator class

(PHP 5 >= 5.1.0)

Introduzione

The EmptyIterator class for an empty iterator.

Sommario dellla classe

EmptyIterator implements Iterator {
/* Metodi */
public void current ( void )
public void key ( void )
public void next ( void )
public void rewind ( void )
public void valid ( void )
}

Indice dei contenuti

add a note add a note

User Contributed Notes 1 note

up
0
Ben
3 months 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