3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Entity { protected $_properties = []; public function &__get($property) { $value = null; if (isset($this->_properties[$property])) { $value =& $this->_properties[$property]; } return $value; } public function __set($property, $value) { $this->_properties[$property] = $value; } } class CustomIterator extends IteratorIterator { public $callback; public function __construct($items) { $this->callback = function ($a) { $a->foo .= ' world'; return $a; }; parent::__construct($items); } public function current() { $c = $this->callback; return $c(parent::current()); } } $entity = new Entity(); $entity->foo = 'hello'; $it = new CustomIterator(new ArrayIterator([$entity])); $it->rewind(); var_dump($it->current());

preferences:
42.46 ms | 411 KiB | 6 Q