3v4l.org

run code in 200+ 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());
based on JiuaG
Output for 7.4.0
object(DateTimeImmutable)#2 (3) { ["date"]=> string(26) "2019-12-02 18:30:30.000319" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } object(DateTimeImmutable)#2 (3) { ["date"]=> string(26) "2019-12-02 18:30:30.000319" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } Fatal error: Uncaught Error: Using $this when not in object context in /in/MDAPb:14 Stack trace: #0 /in/MDAPb(29): Foo::getFoo() #1 {main} thrown in /in/MDAPb on line 14
Process exited with code 255.
Output for 7.1.26 - 7.3.12
Parse error: syntax error, unexpected 'DateTimeImmutable' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /in/MDAPb on line 4
Process exited with code 255.