Spl/interfaces-Phpdoc专题

接口

目录

SPL 提供一系列接口。

可参考预定义接口

接口列表

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

简介

类实现 Countable 可被用于 <span class="function">count 函数.

接口摘要

Countable

class Countable {

/* 方法 */

abstract <span class="modifier">public int <span class="methodname">count ( void )

}

Countable::count

统计一个对象的元素个数

说明

abstract <span class="modifier">public int <span class="methodname">Countable::count ( <span class="methodparam">void )

当使用 count函数作用于一个实现了 <span class="classname">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 <span class="ooclass"> extends Iterator {

/* 方法 */

public Iterator getInnerIterator ( <span class="methodparam">void )

/* 继承的方法 */

abstract <span class="modifier">public mixed <span class="methodname">Iterator::current ( <span class="methodparam">void )

abstract <span class="modifier">public scalar <span class="methodname">Iterator::key ( <span class="methodparam">void )

abstract <span class="modifier">public void <span class="methodname">Iterator::next ( <span class="methodparam">void )

abstract <span class="modifier">public void <span class="methodname">Iterator::rewind ( <span class="methodparam">void )

abstract <span class="modifier">public bool <span class="methodname">Iterator::valid ( <span class="methodparam">void )

}

OuterIterator::getInnerIterator

Returns the inner iterator for the current entry

说明

public Iterator OuterIterator::getInnerIterator ( <span class="methodparam">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 <span class="ooclass"> extends Iterator {

/* 方法 */

public <span class="type">RecursiveIterator <span class="methodname">getChildren ( <span class="methodparam">void )

public bool hasChildren ( <span class="methodparam">void )

/* 继承的方法 */

abstract <span class="modifier">public mixed <span class="methodname">Iterator::current ( <span class="methodparam">void )

abstract <span class="modifier">public scalar <span class="methodname">Iterator::key ( <span class="methodparam">void )

abstract <span class="modifier">public void <span class="methodname">Iterator::next ( <span class="methodparam">void )

abstract <span class="modifier">public void <span class="methodname">Iterator::rewind ( <span class="methodparam">void )

abstract <span class="modifier">public bool <span class="methodname">Iterator::valid ( <span class="methodparam">void )

}

RecursiveIterator::getChildren

Returns an iterator for the current entry

说明

public <span class="type">RecursiveIterator <span class="methodname">RecursiveIterator::getChildren ( <span class="methodparam">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 ( <span class="methodparam">void )

Returns if an iterator can be created for the current entry. <span class="methodname">RecursiveIterator::getChildren.

参数

此函数没有参数。

返回值

Returns true if the current entry can be iterated over, otherwise returns false.

参见

  • RecursiveIterator::getChildren

简介

The Seekable iterator.

接口摘要

SeekableIterator

class SeekableIterator <span class="ooclass"> extends Iterator {

/* 方法 */

abstract <span class="modifier">public void <span class="methodname">seek ( <span class="type">int $position )

/* 继承的方法 */

abstract <span class="modifier">public mixed <span class="methodname">Iterator::current ( <span class="methodparam">void )

abstract <span class="modifier">public scalar <span class="methodname">Iterator::key ( <span class="methodparam">void )

abstract <span class="modifier">public void <span class="methodname">Iterator::next ( <span class="methodparam">void )

abstract <span class="modifier">public void <span class="methodname">Iterator::rewind ( <span class="methodparam">void )

abstract <span class="modifier">public bool <span class="methodname">Iterator::valid ( <span class="methodparam">void )

}

示例 #1 Basic usage

This example demonstrates creating a custom <span class="classname">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 <span class="modifier">public void <span class="methodname">SeekableIterator::seek ( <span class="methodparam">int $position )

Seeks to a given position in the iterator.

参数

position
The position to seek to.

返回值

没有返回值。

错误/异常

Implementations should throw an <span class="classname">OutOfBoundsException if the position is not seekable.

范例

示例 #1 SeekableIterator::seek example

Seek to the item at position 3 in the iterator (<span class="classname">ArrayIterator implements <span class="classname">SeekableIterator).

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

以上例程的输出类似于:

damson

参见

  • SeekableIterator
  • Iterator

本站为非盈利网站,作品由网友提供上传,如无意中有侵犯您的版权,请联系删除