collect-1 可刷新运行 刷新

collect 实现聚合式迭代器

代码:

class collect implements IteratorAggregate
{
    protected $items;
    public function __construct($items = [])
    {
        $this->items=$items;
    }

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

$collect=new collect(['a'=>'A','b'=>'B']);

foreach ($collect as $k=>$v){
    var_dump($v);
}

输出:

string(1) "A"
string(1) "B"