3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Action { /** @var \Iterator */ public $generator; public function __construct(array $data) { $this->generator = new \ArrayIterator($data); $this->generator->x = $this; // circular ref (can be any custom iterator) 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";
Output for 8.4.1 - 8.4.14, 8.5.0 - 8.5.1
Deprecated: Action::limit(): Implicitly marking parameter $limit as nullable is deprecated, the explicit nullable type must be used instead in /in/3nIOH on line 38 Deprecated: Creation of dynamic property ArrayIterator::$x is deprecated in /in/3nIOH on line 11 -- c 1 Array ( [0] => a [1] => b ) -- d 1 Deprecated: Creation of dynamic property ArrayIterator::$x is deprecated in /in/3nIOH on line 11 -- c 1 Array ( [0] => a [1] => b ) -- d 1 Deprecated: Creation of dynamic property ArrayIterator::$x is deprecated in /in/3nIOH on line 11 -- c 1 Array ( [0] => a ) -- d 1 ----------------
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.
Output for 8.2.0 - 8.2.29, 8.3.0 - 8.3.28
Deprecated: Creation of dynamic property ArrayIterator::$x is deprecated in /in/3nIOH on line 11 -- c 1 Array ( [0] => a [1] => b ) -- d 1 Deprecated: Creation of dynamic property ArrayIterator::$x is deprecated in /in/3nIOH on line 11 -- c 1 Array ( [0] => a [1] => b ) -- d 1 Deprecated: Creation of dynamic property ArrayIterator::$x is deprecated in /in/3nIOH on line 11 -- c 1 Array ( [0] => a ) -- d 1 ----------------
Output for 8.1.4 - 8.1.33
-- c 1 Array ( [0] => a [1] => b ) -- d 1 -- c 1 Array ( [0] => a [1] => b ) -- d 1 -- c 1 Array ( [0] => a ) -- d 1 ----------------
Output for 8.1.0 - 8.1.3
-- c 1 Array ( [0] => a [1] => b ) -- d 1 -- c 1 Array ( [0] => a [1] => b ) -- d 1 -- c 1 Array ( [0] => a ) ---------------- -- d 1
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30
-- c 1 Array ( [0] => a [1] => b ) -- d 1 -- c 3 Array ( [0] => a [1] => b ) -- c 1 Array ( [0] => a ) ---------------- -- d 1 -- d 3

preferences:
164.98 ms | 414 KiB | 5 Q