3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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->valTransformCallback ? call_user_func($this->valTransformCallback, $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; }); foreach ($it as $val) { echo $val . "\n"; }
Output for 5.4.0 - 7.1.0
2 3 4
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[', expecting ')' in /in/Cddms on line 72
Process exited with code 255.
Output for 5.1.0 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/Cddms on line 3 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/Cddms on line 30 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/Cddms on line 35 Parse error: syntax error, unexpected '[', expecting ')' in /in/Cddms 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/Cddms on line 3 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/Cddms on line 30 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/Cddms on line 35 Parse error: parse error, unexpected '[', expecting ')' in /in/Cddms 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/Cddms on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting '{' in /in/Cddms on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'{'' in /in/Cddms on line 3
Process exited with code 255.