开发者

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

开发者 https://www.devze.com 2023-01-16 13:13 出处:网络
Quick question really: I\'ve seen in bits of code in the past where the code could call count() on a object t开发者_开发百科hat implements Iterator, for custom Iterator classes.

Quick question really:

I've seen in bits of code in the past where the code could call count() on a object t开发者_开发百科hat implements Iterator, for custom Iterator classes.

I've written a class that implements Iterator and works fine in a foreach loop, but I was just wondering if there was an extra class I had to extend or function I had to implement in order for count() to work on my class, to return a value I want.


I don't have much experience with Iterators, but this looks like what you're looking for:

The Countable interface

Classes implementing Countable can be used with the count() function.


If you only have to do a count on your Iterator class, you can use the iterator_count() function.

0

精彩评论

暂无评论...
验证码 换一张
取 消