php教程

The EmptyIterator class

(PHP 5 >= 5.1.0, PHP 7)

简介

The EmptyIterator class for an empty iterator.

类摘要

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

Table of Contents

User Contributed Notes

Ben 20-Jun-2017 12:42
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();
        }
    }

}
?>

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788