Re: [RFC] Lazy Objects

From: Date: Thu, 06 Jun 2024 06:53:04 +0000
Subject: Re: [RFC] Lazy Objects
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-123529@lists.php.net to get a copy of this message
Hi Arnaud, śr., 5 cze 2024 o 20:08 Arnaud Le Blanc <arnaud.lb@gmail.com> napisał(a): > Hi Larry, > > Thank you for the feedback. > > I think you got the two strategies right. However, there is a use-case > in which an object manages its own laziness by making itself lazy: > > ``` > class C { > public function __construct() { > ReflectionLazyObject::makeLazyGhost($this, $this->init(...)); > } > } > ``` > > This one can not be addressed by a newInstance*() method since the > object to be made lazy already exists. > Did you consider implementing it using some attribute? On constructor like: ``` class C { #[LazyInitialization] public function __construct(private readonly string $foo) { // ... init executes after first use, but all promoted properties are already initialized } } ``` or on specialized initializer: ``` class C { public function __construct(private readonly string $foo) { // do something light } #[LazyInitialization] private function initi(): void { // do something heavy } } ``` I don't know if this is a good example of doing the same thing or if it doesn't limit functionality, but for me, it is way more clean and easier to understand. Cheers, Michał Marcin Brzuchalski

« previous php.internals (#123529) next »