3v4l.org

run code in 300+ PHP versions simultaneously
<?php class TransformingIterator implements \Iterator { /** * @var Iterator */ private $inner; /** * @var callable */ private $valTransformCallback; /** * @var callable */ private $keyTransformCallback; /** * Constructor * * @param Iterator|array $inner * @param callable $valTransformCallback * @param callable $keyTransformCallback * @throws \InvalidArgumentException */ public function __construct($inner, callable $valTransformCallback = null, callable $keyTransformCallback = null) { if ($inner instanceof \Iterator) { $this->inner = $inner; } else if (is_array($inner)) { $this->inner = new \ArrayIterator($inner); } else { throw new \InvalidArgumentException('$inner must be an Iterator or an array'); } $this->valTransformCallback = $valTransformCallback; $this->keyTransformCallback = $keyTransformCallback; } public function current() { return $this->valTransformCallback ? call_user_func($this->valTransformCallback, $this->inner->current()) : $this->inner->current(); } public function next() { $this->inner->next(); } public function key() { return $this->keyTransformCallback ? call_user_func($this->keyTransformCallback, $this->inner->key()) : $this->inner->key(); } public function valid() { return $this->inner->valid(); } public function rewind() { $this->inner->rewind(); } } $it = new TransformingIterator([1,2,3], function ($value) { return $value + 1; }, function ($value) { return $value - 1; }); foreach ($it as $key => $val) { echo $key . '=>' . $val . "\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 TransformingIterator::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/sC6JV on line 42 Deprecated: Return type of TransformingIterator::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/sC6JV on line 49 Deprecated: Return type of TransformingIterator::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/sC6JV on line 54 Deprecated: Return type of TransformingIterator::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/sC6JV on line 61 Deprecated: Return type of TransformingIterator::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/sC6JV on line 66 -1=>2 0=>3 1=>4
Output for 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
-1=>2 0=>3 1=>4
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[', expecting ')' in /in/sC6JV on line 72
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/sC6JV on line 3 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/sC6JV on line 30 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/sC6JV on line 33 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/sC6JV on line 35 Parse error: syntax error, unexpected '[', expecting ')' in /in/sC6JV on line 72
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/sC6JV on line 3 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/sC6JV on line 30 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/sC6JV on line 33 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/sC6JV on line 35 Parse error: parse error, unexpected '[', expecting ')' in /in/sC6JV on line 72
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting '{' in /in/sC6JV on line 3
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/sC6JV on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'{'' in /in/sC6JV on line 3
Process exited with code 255.

preferences:
190.44 ms | 401 KiB | 328 Q