3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class it implements OuterIterator { private $inner; public function __construct(Iterator $inner) { $this->inner = $inner; } public function getInnerIterator() { echo "++ inner\n"; return $this->inner; } public function next() { echo "-- Next\n"; return $this->inner->next(); } public function current() { echo "-- Current\n"; return $this->inner->current(); } public function key() { echo "-- Key\n"; return $this->inner->key(); } public function rewind() { echo "-- Rewind\n"; return $this->inner->rewind(); } public function valid() { echo "-- Valid\n"; return $this->inner->valid(); } } $it = new it(new ArrayIterator(array("foo", "bar"))); foreach ($it as $key => $value) { echo $value."\n"; } $array_a = new ArrayIterator(array('a', 'b', 'c')); $array_b = new ArrayIterator(array('d', 'e', 'f')); $iterator = new AppendIterator; $iterator->append($array_a); $iterator->append($array_b); foreach ($iterator as $current) { echo $current; if ($current=='e') { $iterator->append(new ArrayIterator('g','h')); } }
based on sa4Pf
Output for 7.0.0 - 7.2.0
-- Rewind -- Valid -- Current -- Key foo -- Next -- Valid -- Current -- Key bar -- Next -- Valid abcde Fatal error: Uncaught TypeError: ArrayIterator::__construct() expects parameter 2 to be integer, string given in /in/h4XAh:62 Stack trace: #0 /in/h4XAh(62): ArrayIterator->__construct('g', 'h') #1 {main} thrown in /in/h4XAh on line 62
Process exited with code 255.
Output for 5.3.20 - 5.6.28
-- Rewind -- Valid -- Current -- Key foo -- Next -- Valid -- Current -- Key bar -- Next -- Valid abcde Fatal error: Uncaught exception 'InvalidArgumentException' with message 'ArrayIterator::__construct() expects parameter 2 to be long, string given' in /in/h4XAh:62 Stack trace: #0 /in/h4XAh(62): ArrayIterator->__construct('g', 'h') #1 {main} thrown in /in/h4XAh on line 62
Process exited with code 255.