3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo { private DateTimeImmutable $bar; private ?string $foo; public function getBar(): DateTimeImmutable { return $this->bar ??= new DateTimeImmutable(); } public static function getFoo(): ?string { return $this->foo ??= null; } public static function setFoo(string $foo): self { $this->foo = $foo; return $this; } } $foo = new Foo(); var_dump($foo->getBar()); var_dump($foo->getBar()); var_dump($foo->getFoo()); $foo->setFoo('test'); var_dump($foo->getFoo());

preferences:
55.54 ms | 402 KiB | 5 Q