Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Reasons to use Iterators?

Tags:

iterator

php

I was perusing the manual today and noticed the various iterators. To me, it seems like they are all somewhat pointless; I don't see a reason to use them unless you prefer their syntax, or don't know how to write a recursive function. Are there any reasons to use built-in iterators in PHP over just writing a loop or making a recursive method?

I'm only looking for factual responses, not subjective preferences (ie: I don't care if you think it's more "readable" or "more object-oriented", I want to know if they're faster, offer functionality that can't be achieved when rolling your own, etc.).

like image 587
mrjminer Avatar asked Jun 06 '11 01:06

mrjminer


People also ask

What is the advantage of iterators?

Benefits of Iterators. Use of an iterator simplifies the code and makes it general. Benefits of using this iterator API include: treats variables of all types, sizes, and shapes uniformly, whether they fit in memory or not, even if a single row won't fit in memory.

What are iterators in PHP?

An iterable is any value which can be looped through with a foreach() loop. The iterable pseudo-type was introduced in PHP 7.1, and it can be used as a data type for function arguments and function return values.

What is an iterator and why are iterators necessary?

An iterator is an object (like a pointer) that points to an element inside the container. We can use iterators to move through the contents of the container. They can be visualized as something similar to a pointer pointing to some location and we can access the content at that particular location using them.

What is iterator function?

Iterator in Java is used to traverse each and every element in the collection. Using it, traverse, obtain each element or you can even remove. ListIterator extends Iterator to allow bidirectional traversal of a list, and the modification of elements.


1 Answers

I believe the main reason is versatility. They don't make things more readable and you can accomplish most of what the spl iterators do with a simple foreach.

But iterators can serve as sort of a compacted loop or loop source that you can pass around. You can have more diverse data sources than just a list/array. And the real advantage is that you can wrap or stack them for aggregation.

The naming is somewhat unobvious, but I guess you could use the AppendIterator to for example combine a static list of filenames and the result of a DirectoryIterator. Or otherwise wrap that in a LimitIterator instead of hardwiring that into a for condition. All of these are pretty basic features and can be done manually in a foreach. But at least RegexIterator and FilterIterator seem suitable to reduce some complexity.

But in the end it's really a stylistic choice. It would make most sense if you have custom objects which traverse something (for example a VFS object which can either point to real files or database entries). But even then you could just iterator_to_array convert such a list, and use a normal foreach-over-array.

The only case were it is really imperative to prefer iterators were if the data source is unlimited in size. Unfolded arrays for use in a foreach can consume more memory than an iterator which can retrieve element by element.

like image 78
mario Avatar answered Sep 18 '22 22:09

mario