Using count () for a class that implements Iterator in PHP

Quick question:

I have seen in bits of code in the past where code could call count () on an object that implements Iterator, for custom Iterator classes.

I wrote a class that implements Iterator and works fine in the foreach loop, but I'm just wondering if there is an extra class that I had to extend, or functions that I had to implement, so that the count () function worked on my class to return the value I want.

+4
source share
2 answers

Iterators don't have much experience, but this one looks the way you want:

Counter interface

Using the Countable classes can be used with the count () function.

+11
source

If you just need to count your Iterator class, you can use the iterator_count () function.

+1
source

All Articles