What does it mean When implementing interface use IteratorAggregate or Iterator in its name in php?

209 Views Asked by At

I am new to php and learning it from php.net. As we know when we want to implement Traversable interface we implements IteratorAggregate or Iterator interface with user defined classes that implements the Traversable interface internally. But one thing that is confusing me in the 2nd note on that page (http://php.net/manual/en/class.traversable.php) which says:

When implementing an interface which extends Traversable, make sure to list IteratorAggregate or Iterator before its name in the implements clause.

Can anyone tell what does it says ?

1

There are 1 best solutions below

4
On

Basically, when you create the class, after the implements statement, ensure you list IteratorAggregate or Iterator before the other interfaces in the declaration.

<?php
interface Spam extends \Traversable
{
    // ...
}
final class Ham implements \IteratorAggregate, Spam
{
    public function getIterator()
    {
        return new \ArrayIterator(range(1, 10));
    }
}