PHP Conference Japan 2024

The SeekableIterator interface

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

Introduction

The Seekable iterator.

Interface synopsis

interface SeekableIterator extends Iterator {
/* Methods */
public seek(int $offset): void
/* Inherited methods */
}

Examples

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)

Table of Contents

add a note

User Contributed Notes 4 notes

up
8
svenr at selfhtml dot org
13 years ago
Best method:

<?php

if ($object instanceof SeekableIterator) {
echo
"\$object has method seek()";
}

?>

Please, make use of checking if a particular interface has been implemented to assure that the object you are dealing with definately has the methods you are about to use.

This also works as typehinting:

<?php

class foo {
public function
doSomeSeeking(SeekableIterator $seekMe) {
$seekMe->seek(10); // will work, otherwise Typehint triggers complaints
}
}

?>
up
1
info at ensostudio dot ru
2 years ago
Note: $offset parameter of SeekableIterator::seek() is position in list, not array key.
<?php
$iterator
= new ArrayIterator([1 => "apple", 2 => "banana", 3 => "cherry"]);
echo
$iterator->offsetGet(2); // banana
$iterator->seek(2);
echo
$iterator->current(); // cherry
?>
up
-1
info at ensostudio dot ru
4 years ago
Note: use array_key_exists instead isset!
<?php
public function seek($position)
{
$position = (int) $position;
if (!
array_key_exists($position, array_values($this->array))) {
throw new
OutOfBoundsException('Invalid position to seek: ' . $position);
}
$this->position = $position;
}
?>
up
-2
Anonymous
11 years ago
The code above is missing a closing parenthesis.

<?php
if (!isset($this->array[$position]) {
throw new
OutOfBoundsException("invalid seek position ($position)");
}
?>

should be

<?php
if (!isset($this->array[$position])) { // close here
throw new OutOfBoundsException("invalid seek position ($position)");
}
?>
To Top