3v4l.org

run code in 300+ PHP versions simultaneously
<?php class MyValue { private $value; function get() { return $this->value; } function set($value) { $this->value = $value; } function delete() { $this->value = null; } } class MyIterator implements Iterator { private $data; private $key = 0; function __construct() { $this->data = new MyValue(); } function __destruct() { $this->data->delete(); } function current() { $this->data->set($this->key); return $this->data; } function key() { return $this->key; } function next() { ++$this->key; } function rewind() { $this->key = 0; } function valid() { return $this->key < 10; } } function iterate() { $out = array(); $values = new MyIterator(); foreach ($values as $value) { $out[] = $value; } print_r($out); return $out; } $values = iterate(); print_r($values);

preferences:
52.5 ms | 402 KiB | 5 Q