I've never used generators in PHP before and there are no examples in the documentation that show the return type declaration.
In PhpStorm, there is an error in the IDE when I do this:
public function getDataIncrementally(): void {
yield from [/* some large set of numbers*/];
}
The error is:
Generators may only declare a return type of Generator, Iterator or Traversable, or iterable, void is not permitted.
I can see the inheritance tree is Traversable
-> Iterator
-> Generator
. Meanwhile, iterable
is a new pseudo-type introduced in PHP 7.1.
Would it be appropriate to use iterable
for the return type declaration if I only need to support PHP >= 7.1?
Your return type is Generator
, but you are using void
. Try the following:
public function getDataIncrementally(): \Generator {
yield from [/* some large set of numbers*/];
}