3v4l.org

run code in 300+ PHP versions simultaneously
<?php class IncrementIterator implements Iterator { protected $start; protected $end; protected $current; public function __construct($start, $end) { $this->start = $start; $this->end = $end; } public function rewind() { $this->current = $this->start; } public function valid() { return $this->current <= $this->end; } public function key() { return $this->current; } public function current() { return $this->current; } public function next() { $this->current++; } } class NestedIterator implements Iterator { protected $iterators = array(); protected $lastIndex = -1; protected $iteration = 0; public function addIterator(Iterator $it) { $this->iterators[] = $it; $this->lastIndex++; } public function rewind() { foreach ($this->iterators as $it) { $it->rewind(); } $this->iteration = 0; } public function valid() { return $this->lastIndex >= 0 && $this->iterators[0]->valid(); } public function key() { return $this->iteration; } public function current() { $return = array(); foreach ($this->iterators as $it) { $return[] = $it->current(); } return $return; } public function next() { $this->iteration++; var_dump($this->lastIndex);exit; for ($i = $this->lastIndex; $i >= 0; $i--) { $this->iterators[$i]->next(); if (!$this->iterators[$i]->valid()) { continue; } for ($j = $i + 1; $j <= $this->lastIndex; $j++) { $this->iterators[$j]->rewind(); } } } } $nestedIterator = new NestedIterator(); $nestedIterator->addIterator(new IncrementIterator(0, 26)); $nestedIterator->addIterator(new IncrementIterator(0, 26)); $nestedIterator->addIterator(new IncrementIterator(0, 26)); $nestedIterator->addIterator(new IncrementIterator(0, 26)); foreach ($nestedIterator as $key => $current) { list($a, $b, $c, $d) = $current; echo "[$key] => $a, $b, $c, $d\n"; }
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Deprecated: Return type of IncrementIterator::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Qr1Zh on line 24 Deprecated: Return type of IncrementIterator::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Qr1Zh on line 28 Deprecated: Return type of IncrementIterator::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Qr1Zh on line 20 Deprecated: Return type of IncrementIterator::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Qr1Zh on line 16 Deprecated: Return type of IncrementIterator::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Qr1Zh on line 12 Deprecated: Return type of NestedIterator::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Qr1Zh on line 59 Deprecated: Return type of NestedIterator::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Qr1Zh on line 69 Deprecated: Return type of NestedIterator::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Qr1Zh on line 55 Deprecated: Return type of NestedIterator::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Qr1Zh on line 51 Deprecated: Return type of NestedIterator::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/Qr1Zh on line 43 [0] => 0, 0, 0, 0 int(3)
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
[0] => 0, 0, 0, 0 int(3)
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting '{' in /in/Qr1Zh on line 2
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting '{' in /in/Qr1Zh on line 2
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'{'' in /in/Qr1Zh on line 2
Process exited with code 255.

preferences:
235.72 ms | 401 KiB | 327 Q