3v4l.org

run code in 200+ PHP versions simultaneously
<?php class hello { public $props = array(); function __construct() { $this->props = ['hello' => 5, 'props' => ['keyme' => ['test' => 5]]]; } } $data = new hello(); $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($data), RecursiveIteratorIterator::SELF_FIRST); echo "Expect to see all keys in ->props here: \n"; foreach($iterator as $k=>$v) { echo $k . "\n"; } echo "Reimplemented RecursiveArrayIterator from source code here: http://fossies.org/dox/php-7.0.11-src/recursivearrayiterator_8inc_source.html \n"; class RecursiveArray extends ArrayIterator implements RecursiveIterator { function hasChildren() { return is_array($this->current()); } function getChildren() { if ($this->current() instanceof self) { return $this->current(); } if (empty($this->ref)) { $this->ref = new ReflectionClass($this); } return $this->ref->newInstance($this->current()); } private $ref; } $iterator = new RecursiveIteratorIterator(new RecursiveArray($data), RecursiveIteratorIterator::SELF_FIRST); echo "Expect to see all keys in ->props here: \n"; foreach($iterator as $k=>$v) { echo $k . "\n"; }
based on IBrNH
Output for 5.6.0 - 5.6.38, 7.1.14 - 7.1.25, 7.2.2 - 7.3.1
Expect to see all keys in ->props here: props hello props keyme test Reimplemented RecursiveArrayIterator from source code here: http://fossies.org/dox/php-7.0.11-src/recursivearrayiterator_8inc_source.html Expect to see all keys in ->props here: props hello props keyme test
Output for 7.0.0 - 7.1.13, 7.2.0 - 7.2.1
Expect to see all keys in ->props here: props Reimplemented RecursiveArrayIterator from source code here: http://fossies.org/dox/php-7.0.11-src/recursivearrayiterator_8inc_source.html Expect to see all keys in ->props here: props hello props keyme test