- var_dump: documentation ( source)
- iterator_to_array: documentation ( source)
<?php
class test
{
private $whatever = false;
public function __construct()
{
$a = iterator_to_array($this->firstIterator());
$b = iterator_to_array($this->secondIterator($this->firstIterator()));
$c = iterator_to_array($this->thirdIterator($this->firstIterator()));
$this->whatever = true;
$d = iterator_to_array($this->firstIterator());
$e = iterator_to_array($this->secondIterator($this->firstIterator()));
$f = iterator_to_array($this->thirdIterator($this->firstIterator()));
var_dump([$a, $b, $c, $d, $e, $f]);
}
public function firstIterator(): \Iterator
{
yield 'a'=> 1;
yield 'b'=> 2;
yield 'c'=> 3;
}
public function secondIterator(\Iterator $first): \Iterator
{
if ($this->whatever === true)
{
return $first;
}
foreach($first as $key=>$value)
{
yield $key => $value * $value;
}
}
public function thirdIterator(\Iterator $first): \Iterator
{
if ($this->whatever === true)
{
return $first;
}
return $this->thirdSubIterator($first);
}
public function thirdSubIterator(\Iterator $first): \Iterator
{
foreach($first as $key=>$value)
{
yield $key => $value * $value;
}
}
}
new test();