Niklas Keller excellent blog post focusing on generators in PHP. Generators are special functions in PHP. Whenever a function contains yield, it’s no longer a normal function anymore, but always returns a Generator. Generators have been added to PHP in version 5.5, yet they have received rather low attention.
It is important to notice that generators don’t execute anything until you start using the returned generator. We have an interruptible function now that we can pause and resume at any yield. This enables writing lazy functions that do just as much as the consumer needs. You could build something that reads all users via the GitHub API. These are paginated, but you could hide that detail and fetch new pages as needed.
Plenty of code examples will help you to get up to the speed with generators and what is the main difference between them and non-iterator implementation using arrays. Generators have way more use cases than many people think. Could not agree more!
[Read More]