- Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- string(16) "Saying something" string(21) "Saying something lazy"
<?php
class Foo {
private $foo;
private $lazyLoad;
public function saySomething() {
return 'Saying ' . $this->foo;
}
private function __construct() {
}
public static function new() : self {
$instance = new self();
$instance->foo = 'something';
return $instance;
}
public static function newLazy() : self {
$instance = new self();
$instance->lazyLoad = function () use ($instance) {
$instance->foo = 'something lazy';
};
unset($instance->foo);
return $instance;
}
public function __get($name) {
($this->lazyLoad)();
return $this->foo;
}
}
$foo = Foo::new();
var_dump($foo->saySomething());
$lazyFoo = Foo::newLazy();
var_dump($lazyFoo->saySomething());