接口

目录

SPL 提供一系列接口。

可参考预定义接口

接口列表

  • Countable
  • OuterIterator
  • RecursiveIterator
  • SeekableIterator
  • SplObserver
  • SplSubject

简介

类实现 Countable 可被用于 count 函数.

接口摘要

Countable

class Countable {

/* 方法 */

abstract public int count ( void )

}

Countable::count

统计一个对象的元素个数

说明

abstract public int Countable::count ( void )

当使用 count函数作用于一个实现了 Countable的对象时这个方法会被执行.

参数

此函数没有参数。

返回值

The custom count as an integer.

Note:

The return value is cast to an integer.

范例

示例 #1 Countable::count example

<?php
class myCounter implements Countable {
    public function count() {
        static $count = 0;
        return ++$count;
    }
}

$counter = new myCounter;

for($i=0; $i<10; ++$i) {
    echo "I have been count()ed " . count($counter) . " times\n";
}
?>

以上例程的输出类似于:

I have been count()ed 1 times
I have been count()ed 2 times
I have been count()ed 3 times
I have been count()ed 4 times
I have been count()ed 5 times
I have been count()ed 6 times
I have been count()ed 7 times
I have been count()ed 8 times
I have been count()ed 9 times
I have been count()ed 10 times

简介

Classes implementing OuterIterator can be used to iterate over iterators.

接口摘要

OuterIterator

class OuterIterator extends Iterator {

/* 方法 */

public Iterator getInnerIterator ( void )

/* 继承的方法 */

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 bool Iterator::valid ( void )

}

OuterIterator::getInnerIterator

Returns the inner iterator for the current entry

说明

public Iterator OuterIterator::getInnerIterator ( void )

Returns the inner iterator for the current iterator entry.

参数

此函数没有参数。

返回值

The inner iterator for the current entry.

简介

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

接口摘要

RecursiveIterator

class RecursiveIterator extends Iterator {

/* 方法 */

public RecursiveIterator getChildren ( void )

public bool hasChildren ( void )

/* 继承的方法 */

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 bool Iterator::valid ( void )

}

RecursiveIterator::getChildren

Returns an iterator for the current entry

说明

public RecursiveIterator RecursiveIterator::getChildren ( void )

Returns an iterator for the current iterator entry.

参数

此函数没有参数。

返回值

An iterator for the current entry.

参见

  • RecursiveIterator::hasChildren

RecursiveIterator::hasChildren

Returns if an iterator can be created for the current entry

说明

public bool RecursiveIterator::hasChildren ( void )

Returns if an iterator can be created for the current entry. RecursiveIterator::getChildren.

参数

此函数没有参数。

返回值

Returns TRUE if the current entry can be iterated over, otherwise returns FALSE.

参见

  • RecursiveIterator::getChildren

简介

The Seekable iterator.

接口摘要

SeekableIterator

class SeekableIterator extends Iterator {

/* 方法 */

abstract public void seek ( int $position )

/* 继承的方法 */

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 bool Iterator::valid ( void )

}

示例 #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();
}
?>

以上例程的输出类似于:

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

SeekableIterator::seek

Seeks to a position

说明

abstract public void SeekableIterator::seek ( int $position )

Seeks to a given position in the iterator.

参数

position
The position to seek to.

返回值

没有返回值。

错误/异常

Implementations should throw an OutOfBoundsException if the position is not seekable.

范例

示例 #1 SeekableIterator::seek example

Seek to the item at position 3 in the iterator (ArrayIterator implements SeekableIterator).

<?php
$array = array("apple", "banana", "cherry", "damson", "elderberry");
$iterator = new ArrayIterator($array);
$iterator->seek(3);
echo $iterator->current();
?>

以上例程的输出类似于:

damson

参见

  • SeekableIterator
  • Iterator