Iterator: Provide a way to access elements of a collection sequentially without exposing the underlying data structure. The Iterator pattern gives a standard way to go through the items of a collection one by one, without knowing how the collection stores them. The same foreach works for an array, a database result or a file read line by line.
PHP supports it with the Iterator and IteratorAggregate interfaces, and generators with yield make writing iterators easy. JavaScript has iterators and for...of.
What should you keep in mind with Iterator?
The real power is lazy processing. A generator can read a two-gigabyte CSV one line at a time, keeping memory use tiny. Laravel's lazy() and cursor() do the same for database results. Whenever you process large data sets, an iterator that hands out one item at a time is usually better than loading everything into an array first.
Implement proven backend architecture patterns including Clean Architecture, Hexagonal Architecture, and Domain-Driven Design. Use this skill when designing clean architecture for a new microservice, when refactoring a monolith to use bounded contexts, when implementing hexagonal or onion architecture patterns, or when debugging dependency cycles between application layers.
'Cloud design patterns for distributed systems architecture covering 42 industry-standard patterns across reliability, performance, messaging, security, and deployment categories. Use when designing, reviewing, or implementing distributed system architectures.'