3

I am trying to understand something about ArrayObject and ArrayIterator classes

ArrayObject: This class implements the IteratorAggregate and Traversable interfaces. Since IteratorAggregate extends Traversable itself, why does ArrayObject implement both when it can just implement IteratorAggregate?

ArrayIterator: As above, this class implements both Iterator and Traversable which Iterator extends.

Why do these classes implement two interfaces when they could get away with implementing the one that extends Traversable?

David Harkness
  • 35,992
  • 10
  • 112
  • 134
Aviel Fedida
  • 4,004
  • 9
  • 54
  • 88
  • 3
    I suspect those links are only there to make the manual clearer and more *traversable* (ha) - ironically in your case that seems to have had the total opposite effect. I think you are correct in that one would more correctly say that they implement the `IteratorAggregate` and `Iterator` interfaces respectively. – DaveRandom Jul 06 '12 at 16:17
  • ...although [it would seem](http://codepad.viper-7.com/r4FfNY) that you can write your code in this seemingly conflicting way and PHP will not complain about it. – DaveRandom Jul 06 '12 at 16:24
  • I don't think Java would complain either but haven't tested it. It's like declaring unchecked exceptions: superfluous but not an error. – David Harkness Jul 06 '12 at 16:26

1 Answers1

1

According to the SPL source code, ArrayObject doesn't implement Traversable directly:

class ArrayObject implements IteratorAggregate, ArrayAccess, Countable
{
    ...
}

This matches the SPL documentation. I believe DaveRandom is correct.

David Harkness
  • 35,992
  • 10
  • 112
  • 134
  • I looked at the link and it looks like ArrayObject and ArrayIterator Classes both is not implements Traversable Interface directly, – Aviel Fedida Jul 06 '12 at 16:36