PHP – Make Iterator with PHP Class

PHP and iterators…

Violin and boxing…

Anyway, it is possible and the possibility is since some years ago.

In this article I will simply show how to make an iterator with a PHP class. And I would iterate in a table, with different background colors per row. And that is it!

iterator

In order to make an iterator with Php class, we need to define 5 functions in the class:

  1. current()
  2. key()
  3. next()
  4. rewind()
  5. valid()

Once this is done, the iterator is ready :). And we may access it with the help of a foreach array.

Here comes the code:

Use responsibly. In Github.

🙂

Tagged with: , ,