Generators are a relatively unexplored feature of PHP. Coming about in PHP 5.5, Generators provide for the ability to write Iterator style code without a lot of upfront work. In addition to that Generators provide for some amazing abilities to write coroutines in PHP that we previously didn't have. Generators are at their core a tool to enable us to write code that can solve entire classes of problems that beforehand would have been exceedingly hard to do before hand. Perhaps most importantly, generators are fun to explore.


Comments are closed.

Philip Sharp at 13:05 on 27 Jul 2018

Great progression from the simplest generator through each additional feature. Would be nice if the final coroutine example could fit on one slide--it's tricky to understand.

This was an absolute firehose, but I think the room was having a hard time keeping up with Justin. I spent most of the talk trying to think "where would this be really useful", only to have the talk end on (spoilers) "so, where is this stuff really useful? Well, not many places, to be honest."

Rather than fizzbuzz and counting, even fake user-space code might make the examples more accessible.

I was able to follow well, but I have the advantage of having dealt with much of this in Python, where generators are more of a norm. And despite that, I still learned things about generators in PHP.

What was hard to follow for me was the examples where there was a "yield 'return-val'" mixed with send() calls. It was much clearer to see what was happening with numbers being yielded instead, because it's obvious when "2" and "3" come back, that "1" was lost.