1. Generators provide an easier way to implement simple object iteration. The performance overhead and complexity are significantly reduced compared to defining classes.
2. Generators allow writing code in a foreach block to iterate over a set of data without creating an array in memory, which would hit the memory limit or take up considerable processing time.
Example
public function testYield($nums) { foreach($nums as $num) { yield $num; } } public function index() { foreach($this->testYield([1,2,4,5]) as $num) { dump($num); } die(); }
The above is an introduction to generators in php. I hope it will be helpful to everyone. More PHP learning guide: source code network