In PHP 7.1 there is a new iterable psudo-type which abstracts arrays and Traversable
objects.
Suppose that in my code I have a class like the following:
class Foo
{
private $iterable;
public function __construct(iterable $iterable)
{
$this->iterable = $iterable;
}
public function firstMethod()
{
foreach ($this->iterable as $item) {...}
}
public function secondMethod()
{
foreach ($this->iterable as $item) {...}
}
}
This works fine is $iterable
is an array or an Iterator
, except when $iterable
is a Generator
. In that case in fact, calling firstMethod()
and then secondMethod()
would produce the following Exception: Cannot traverse an already closed generator
.
Is there a way to avoid this issue?