<?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();
- Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
- array(6) {
[0]=>
array(3) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
}
[1]=>
array(3) {
["a"]=>
int(1)
["b"]=>
int(4)
["c"]=>
int(9)
}
[2]=>
array(3) {
["a"]=>
int(1)
["b"]=>
int(4)
["c"]=>
int(9)
}
[3]=>
array(3) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
}
[4]=>
array(0) {
}
[5]=>
array(3) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
}
}
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
array(6) {
[0]=>
array(3) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
}
[1]=>
array(3) {
["a"]=>
int(1)
["b"]=>
int(4)
["c"]=>
int(9)
}
[2]=>
array(3) {
["a"]=>
int(1)
["b"]=>
int(4)
["c"]=>
int(9)
}
[3]=>
array(3) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
}
[4]=>
array(0) {
}
[5]=>
array(3) {
["a"]=>
int(1)
["b"]=>
int(2)
["c"]=>
int(3)
}
}
preferences:
202.11 ms | 402 KiB | 180 Q