3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); /** @template-covariant T */ class Store { /** @param T $value */ public function __construct(private $value) { } /** @return T */ public function getValue() { return $this->value; } } /** @template-covariant T */ class LazyValue { /** @var Store<T>|null */ private ?Store $store = null; /** @param Closure(): T $callable */ public function __construct(private Closure $callable) { } /** @return T */ public function getValue() { $store = $this->store ??= $this->doGetStore(); return $store->getValue(); } /** @return Store<T> */ private function doGetStore(): Store { $callable = $this->callable; $value = $callable(); return new Store($value); } } //------------------------ using it --------------------- function slow(): int { echo 'Doing slow calculation once'; return random_int(1, 42); } $lazy = new LazyValue(fn() => slow()); $lazy->getValue(); // prints: 'Doing slow calculation once', returns 'int' $lazy->getValue(); // does not print anything, only retuns 'int'

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.1.110.0050.00317.41

preferences:
33.94 ms | 403 KiB | 5 Q