Hi, this is Jiayong Ou's blog about programming and other things he deems interesting.

He's also on Twitter as @jiayongou and short tidbits of his discoveries on the world wide web are on orly.ch. There are also pages for his other online activies, how to get in touch with him, his projects and about his person.

Instant object iterator in PHP

If you want to have a class iterate over an array member:

class Foo implements IteratorAggregate {
    private $data = array();

    public function add($entry) {
        $data[] = $entry;

    public function getIterator() {
        return new ArrayIterator($this->data)

$foo = new Foo;

foreach($foo as $val) {
    echo "{$val}\n";



That's the most common use case for the Traversible interface in PHP I'm encountering and it's much easier than implement the whole Iterator interface.