3v4l.org

run code in 300+ PHP versions simultaneously
<?php class LazyValue { public function __construct( private Closure $lazy, ){} public function getValue() { static $value = ($this->lazy)(); return $value; } } $t1 = new LazyValue(function () { echo "slow\n"; return "42\n"; }); echo $t1->getValue(); echo $t1->getValue(); echo $t1->getValue();
Output for 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
slow 42 42 42
Output for 8.1.0 - 8.1.34, 8.2.0 - 8.2.30
Fatal error: Constant expression contains invalid operations in /in/Dfn5j on line 11
Process exited with code 255.

preferences:
91.2 ms | 407 KiB | 5 Q