I know that this can be super easily archieved without generators, however I want to understand generators better. Therefore please don't suggest using something else.
I've got a class that generates filenames for screenshots (selenium):
class ScreenshotName
{
private $counter = 0;
public function screenshotNameIterator()
{
while(true) {
yield sprintf("screenshot-%s-%s.png", date("Y-m-d\\TH:i:s"), ++$this->counter);
}
}
}
Now my question is: can I use such a generator in any other context than a foreach loop? e.g.
(new ScreenshotName())->screenshotNameIterator()->next()
for me this always returns null, and if I debug, it never enters the generator method. Also the PHP docs don't really mention this.
So my question is: is there a documented way to use a generator in a different context than a for-loop?