SPL IteratorAggregate

Introduction

Iterator Aggregation allows you to develop without rewriting a lot of the same code for classes. It is simple to implement with only one method in the interface. The method getIterator must return an Iterator object for it to work in foreach.

Interface

interface IteratorAggregate
{
   function getIterator();
}

Example

You can aggregate to PHP extensions that implement a iterator object. If you need to pass XML, you can create a new SimpleXML class giving XML iteration without having to implement it yourself.

class XmlPageViewer implements IteratorAggregate
{
    // My Methods here	 

    public function getIterator()
    {
        return new SimpleXMLElement($this->xml);
    }
}

Possibly Related Posts:


Comments are closed.