UP | HOME

php_spl常用接口

Table of Contents

Countable

简介

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

接口摘要

 Countable {
/* 方法 */
abstract public count ( void ) : int
}

示例

<?php
/**
 Countable {

abstract public count ( void ) : int
}

类实现 Countable 可被用于 count() 函数. 
//必须实现count,不然报错
PHP Fatal error:  Class Demo contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Countable::count) 
*/

class Demo implements Countable {

    protected $arr = [];

    public function insertItemToArr($item) {
        array_push($this->arr, $item);
    }
    
    public function count(){
        return count($this->arr);
    }
    
}


$demo = new Demo();
$demo->insertItemToArr(1);
$demo->insertItemToArr(2);
$demo->insertItemToArr("abc");
echo count($demo).PHP_EOL;
$demo->insertItemToArr(4);
$demo->insertItemToArr(5);
echo count($demo).PHP_EOL;

Iterator

简介

可在内部迭代自己的外部迭代器或类的接口。

接口摘要

 Iterator extends Traversable {
/* 方法 */
abstract public current ( void ) : mixed
abstract public key ( void ) : scalar
abstract public next ( void ) : void
abstract public rewind ( void ) : void
abstract public valid ( void ) : bool
}

示例

class myIterator implements Iterator {
    private $position = 0;
    private $array = array(
    "firstelement",
    "secondelement",
    "lastelement",
    );

    public function __construct() {
        $this->position = 0;
    }

    //第一次循环迭代前调用
    function rewind() {
        var_dump(__METHOD__);
        $this->position = 0;
    echo PHP_EOL;
    }

    function current() {
        var_dump(__METHOD__);
    var_dump("current() : ".$this->array[$this->position]);
        return $this->array[$this->position];
    }

    function key() {
        var_dump(__METHOD__);
    var_dump("key() : ".$this->position);
        return $this->position;
    }

    //每次循环迭执行
    function next() {
        var_dump(__METHOD__);
        ++$this->position;
    }

    //每次循环迭执行,并根据返回 true 或 false 来判断是否结束循环
    function valid() {
        var_dump(__METHOD__);
    var_dump(isset($this->array[$this->position]));
        return isset($this->array[$this->position]);
    }
}

$it = new myIterator();

foreach($it as $key => $value) {
    //var_dump($key, $value);
    //echo "\n";
    echo "****** key={$key}, value={$value} ******".PHP_EOL;
    echo PHP_EOL;
}

IteratorAggregate

简介

创建外部迭代器的接口。

接口摘要

 IteratorAggregate extends Traversable {
/* 方法 */
abstract public getIterator ( void ) : Traversable
}

示例

class myData implements IteratorAggregate {
    public $property1 = "Public property one";
    public $property2 = "Public property two";
    public $property3 = "Public property three";

    public function __construct() {
        $this->property4 = "last property";
    }

    public function getIterator() {
        return new ArrayIterator($this);
    }
}

$obj = new myData;

foreach($obj as $key => $value) {
    var_dump($key, $value);
    echo "\n";
}

First created: 2020-02-22 18:02:33
Last updated: 2021-11-25 Thu 23:22
Power by Emacs 27.1 (Org mode 9.4)
© 2017 – 2021 by josephzeng