phpiteratorphp-7php-5.3traversable

Explanation of the Return Values of the IteratorAggregate::getIterator


http://php.net/manual/en/iteratoraggregate.getiterator.php

Return Values - "An instance of an object implementing Iterator or Traversable"

1) So what gets returned is always an instance of an object, correct? (I am assuming yes, but I want to make sure).

By this url: http://php.net/manual/en/class.iteratoraggregate.php

IteratorAggregate extends Traversable. Somebody also told me that the Iterator extends the Traversable as well. The Traversable is more of an internal thing (written in C I am assuming) and it is not accessible from the level of PHP.

The Iterator I am familiar with, it is the current, key, next, rewind, valid "thing". So what the getIterator returns can be implementing this and if not this it will be implementing something that originates from the Traversable 100% of the time? And the Iterator would not be originating from the Traversable then?

I mean, is this worder correctly, or is there something that I dont understand?


Solution

  • Some of the classes in the extensions implement only Traversable, not the Iterator.

    List of these can be found here: https://github.com/hakre/Iterator-Garden/blob/master/TRAVERSABLES.md

    Code example (Traversable yes, and not the Iterator): https://eval.in/988102