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";
Output for 8.4.1 - 8.4.14, 8.5.0
Deprecated: Action::limit(): Implicitly marking parameter $limit as nullable is deprecated, the explicit nullable type must be used instead in /in/YQIVJ on line 48 -- 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.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.1.4 - 8.1.33, 8.2.10 - 8.2.29, 8.3.0 - 8.3.28
-- 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.28, 8.0.17
-- 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:
50.87 ms | 410 KiB | 5 Q