3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Action { /** @var \Iterator */ public $generator; public function __construct(array $data) { $this->generator = new class($this, $data) extends \ArrayIterator { /** @var Action */ public $action; public function __construct(Action $action, array $data) { $this->action = $action; // circular ref (can be any custom iterator) parent::__construct($data); } }; echo '-- c ' . spl_object_id($this) . "\n"; } public function __destruct() { echo '-- d ' . spl_object_id($this) . "\n"; } /** * @return $this */ public function filter() { $filterFx = function ($row) { return $row !== $this; // always true, dummy use $this }; $this->generator = new \CallbackFilterIterator($this->generator, $filterFx); $this->generator->rewind(); return $this; } /** * @return $this */ public function limit(int $limit = null, int $offset = 0) { $this->generator = new \LimitIterator($this->generator, $offset, $limit ?? -1); $this->generator->rewind(); return $this; } } $action = new Action(['a', 'b']); print_r(iterator_to_array($action->generator)); $action = null; gc_collect_cycles(); $action = new Action(['a', 'b']); $action->filter(); print_r(iterator_to_array($action->generator)); $action = null; gc_collect_cycles(); $action = new Action(['a', 'b']); $action->filter()->limit(1); print_r(iterator_to_array($action->generator)); $action = null; gc_collect_cycles(); echo '----------------' . "\n";

preferences:
27.63 ms | 407 KiB | 5 Q