TP5.1 源码窥探之了解一下容器实现的三个接口

继上一篇自动加载,我们接着往下看,当系统运行至Container::get('app')->run()->send()这一行时,实现Container容器类的自动加载,可以看它的设计模式,使用到了单例模式,注册树模式。当你看它代码的时候不难发现它实现了ArrayAccess, IteratorAggregate, Countable三个接口。下面就了解一下这些接口。

ArrayAccess

官方的描述是:提供像访问数组一样访问对象的能力的接口。也就是向下面这样可以访问数组的方式访问对象。

$obj = new Obj();
echo $obj['name'];

既然是接口类,那么容器类必须要实现该接口的所有方法。

// 检查一个偏移位置是否存在
public function offsetExists($key);
//获取一个偏移位置的值
public function offsetGet($key);
//设置一个偏移位置的值
public function offsetSet($key,$val);
//复位一个偏移位置的值
public function offsetUnset($key);

譬如:Obj.php

namespace note;
use ArrayAccess;

class Obj implements ArrayAccess {
    private $container = array();
    public function __construct() {
        $this->container = array(
            "one"   => 1,
            "two"   => 2,
            "three" => 3,
        );
    }
    public function offsetSet($offset, $value) {
        if (is_null($offset)) {
            $this->container[] = $value;
        } else {
            $this->container[$offset] = $value;
        }
    }
    public function offsetExists($offset) {
        return isset($this->container[$offset]);
    }
    public function offsetUnset($offset) {
        unset($this->container[$offset]);
    }
    public function offsetGet($offset) {
        return isset($this->container[$offset]) ? $this->container[$offset] : null;
    }
}

index.php

$obj = new Obj;
var_dump(isset($obj["two"])); // true
var_dump($obj["two"]); // 2
unset($obj["two"]);
var_dump(isset($obj["two"])); // fasle
$obj["two"] = "A value";
var_dump($obj["two"]); // A value
$obj[] = 'Append 1';
$obj[] = 'Append 2';
$obj[] = 'Append 3';
print_r($obj);

Countable

官方解释是: 统计一个对象的元素个数,实现该接口需要实现count函数。

public function count();

Obj.php

namespace note;
use Countable;

class Obj implements Countable {
    public function count()
    {
        return 111;
    }
}

index.php

$obj = new Obj();
$res1 = count($obj);
$res2 = $obj->count();
var_dump($res1,$res2);// 111 111

IteratorAggregate

官方解释:创建外部迭代器的接口,需要实现getIterator方法

public function getIterator();

举例
MyData.php

namespace note;

use IteratorAggregate;
use ArrayIterator;

class MyData implements IteratorAggregate {
    public    $property1 = "public property one";
    protected $property2 = "protected property two";
    private   $property3 = "private property three";
    protected $bind = ['a'=>'animal','b'=>'banana'];

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

    public function getIterator() {
        return new ArrayIterator($this);
       // return new ArrayIterator($this->bind);
    }

    public function method()
    {
        return '看看方法会不会进入迭代器,还是只是属性才进入迭代器';
    }
}

index.php

$obj = new MyData();
//可以先判断是否可以迭代
if( $obj instanceof \Traversable){
    //像数组一样遍历一个对象,因为实现了迭代器接口,并且只有公有属性才会被迭代
    foreach($obj as $key => $value){
        var_dump($key,$value);
    }
}

注意:虽然实现了迭代器接口,但仍不可以使用count函数统计,需要实现Countable接口。

本作品采用《CC 协议》,转载必须注明作者和本文链接
今年不学习,明天惨唧唧。
zs4336
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!