3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php trait OuterIteratorTrait { private $inner; /** * @return bool */ function isEmpty() { $iterator = clone $this->inner; $iterator->rewind(); return !$iterator->valid(); } function rewind() { $this->inner->rewind(); } function valid() { return $this->inner->valid(); } function next() { $this->inner->next(); } function current() { return $this->inner->current(); } function key() { return $this->inner->key(); } } class MappingIterator implements Iterator { use OuterIteratorTrait; private $mapper; function __construct(\Iterator $i, callable $f) { $this->inner = $i; $this->mapper = $f; } function current() { return call_user_func($this->mapper, $this->inner->current(), $this->key()); } } $i = new ArrayIterator(array(0, 2, 4)); $mapper = new MappingIterator($i, function($val){ return 2 * $val;}); var_dump(iterator_to_array($mapper));
Output for 5.4.0 - 7.1.0
array(3) { [0]=> int(0) [1]=> int(4) [2]=> int(8) }
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.3.29
Parse error: syntax error, unexpected T_STRING in /in/F29gc on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STRING in /in/F29gc on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/F29gc on line 3
Process exited with code 255.