略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: EmptyIterator

2024-12-22

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(): bool
}

目录

add a noteadd a note

User Contributed Notes 1 note

up
14
Ben
5 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();
        }
    }

}
?>

官方地址:https://www.php.net/manual/en/class.emptyiterator.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3