RegexIterator::setPregFlags
Countable
SPL
PHP Manual

# Interfaces

Table of Contents

- Countable — The Countable interface
  - Countable::count — Count elements of an object
- OuterIterator — The OuterIterator interface
  - OuterIterator::getInnerIterator — Returns the inner iterator for the current entry.
- RecursiveIterator — The RecursiveIterator interface
  - RecursiveIterator::getChildren — Returns an iterator for the current entry.
  - RecursiveIterator::hasChildren — Returns if an iterator can be created fot the current entry.
- SeekableIterator — The SeekableIterator interface
  - SeekableIterator::seek — Seeks to a position

SPL provides a set of interfaces.

See also the Predefined Interfaces and Classes

## Interface list

- Countable
- OuterIterator
- RecursiveIterator
- SeekableIterator
- SplObserver
- SplSubject

# The Countable interface

## Introduction

Classes implementing Countable can be used with the count function.

## Interface synopsis

Countable
class Countable {
/* Methods */
abstract public int count ( void )
}

# The OuterIterator interface

## Introduction

Classes implementing OuterIterator can be used to iterate over iterators.

## Interface synopsis

OuterIterator
class OuterIterator extends Iterator {
/* Methods */
public Iterator getInnerIterator ( void )
/* Inherited methods */
abstract public mixed Iterator::current ( void )
abstract public scalar Iterator::key ( void )
abstract public void Iterator::next ( void )
abstract public void Iterator::rewind ( void )
abstract public boolean Iterator::valid ( void )
}

# The RecursiveIterator interface

## Introduction

Classes implementing RecursiveIterator can be used to iterate over iterators recursively.

## Interface synopsis

RecursiveIterator
class RecursiveIterator extends Iterator {
/* Methods */
public RecursiveIterator getChildren ( void )
public bool hasChildren ( void )
/* Inherited methods */
abstract public mixed Iterator::current ( void )
abstract public scalar Iterator::key ( void )
abstract public void Iterator::next ( void )
abstract public void Iterator::rewind ( void )
abstract public boolean Iterator::valid ( void )
}

# The SeekableIterator interface

## Introduction

The Seekable iterator.

## Interface synopsis

SeekableIterator
class SeekableIterator extends Iterator {
/* Methods */
abstract public void seek ( int `$position` )
/* Inherited methods */
abstract public mixed Iterator::current ( void )
abstract public scalar Iterator::key ( void )
abstract public void Iterator::next ( void )
abstract public void Iterator::rewind ( void )
abstract public boolean Iterator::valid ( void )
}

Example #1 Basic usage

This example demonstrates creating a custom SeekableIterator, seeking to a position and handling an invalid position.

`<?php
class MySeekableIterator implements SeekableIterator {
private $position;
private $array = array(
"first element",
"second element",
"third element",
"fourth element"
);
/* Method required for SeekableIterator interface */
public function seek($position) {
if (!isset($this->array[$position]) {
throw new OutOfBoundsException("invalid seek position ($position)");
}
$this->position = $position;
}
/* Methods required for Iterator interface */
public function rewind() {
$this->position = 0;
}
public function current() {
return $this->array[$this->position];
}
public function key() {
return $this->position;
}
public function next() {
++$this->position;
}
public function valid() {
return isset($this->array[$this->position]);
}
}
try {
$it = new MySeekableIterator;
echo $it->current(), "\n";
$it->seek(2);
echo $it->current(), "\n";
$it->seek(1);
echo $it->current(), "\n";
$it->seek(10);
} catch (OutOfBoundsException $e) {
echo $e->getMessage();
}
?>`

The above example will output something similar to:

    first element
    third element
    second element
    invalid seek position (10)