Chapter 9. Generator Enhancements
Generators were my favorite feature in PHP 5.5, and the new changes in PHP 7.0 are definitely close to the top of my list again.
Generator Return Values
In PHP 5.5, if a generator function has a return statement followed by an expression (e.g., return true;), it would result in a parse error.
With the “Generator Return Expressions” RFC, this has now changed. As with the Closure class between 5.3 and 5.4, the Generator class that provides the magic for generators has moved from an implementation detail to a concrete implementation with the addition of the Generator->getReturn() method.
Not only will it no longer cause a parse error, but the Generator->getReturn() method will allow you to retrieve the value returned by any return statement inside the generator. Example 9-1 shows this new functionality.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access