3v4l.org

run code in 500+ PHP versions simultaneously
<?php // storage class, PHP 8.3 needed class LazyValue { public function __construct(private Closure $lazy){} public function getValue() { static $value = ($this->lazy)(); return $value; } } // use $l1 = new LazyValue(function () { echo "slow\n"; return "42\n"; }); echo $l1->getValue(); echo $l1->getValue(); echo $l1->getValue();
Output for 8.3.0 - 8.3.33, 8.4.1 - 8.4.24, 8.5.0 - 8.5.9
slow 42 42 42
Output for 8.2.31 - 8.2.33
Fatal error: Constant expression contains invalid operations in /in/XpdlC on line 9
Process exited with code 255.

preferences:
22.07 ms | 778 KiB | 4 Q