<?php class Foo { public function __construct() { echo 'foo'; } } class Bar { public function __construct() { echo 'bar'; } } class Build { private ?Foo $foo = null; private ?Bar $bar = null; function foo(): Foo { return $this->foo ?? new Foo(); } function bar(): Bar { return $this->bar ??= new Bar(); } } $build = new Build(); $build->foo(); $build->foo(); $build->bar(); $build->bar();
You have javascript disabled. You will not be able to edit any code.