3v4l.org

run code in 300+ PHP versions simultaneously
<?php class myData implements IteratorAggregate { public $property1 = "Public property one"; public $property2 = "Public property two"; public $property3 = "Public property three"; public function __construct() { $this->property4 = "last property"; } public function getIterator() { return new ArrayIterator($this); } } $obj = new myData; echo "foreach (new \$obj)\n"; foreach($obj as $key => $value) { var_dump($key, $value); echo "\n"; } echo "\nforeach (new ArrayIterator(\$obj))\n"; foreach(new ArrayIterator($obj) as $key => $value) { var_dump($key, $value); echo "\n"; }

preferences:
23.94 ms | 404 KiB | 5 Q