Hi Arnaud,

śr., 5 cze 2024 o 20:08 Arnaud Le Blanc <arnaud...@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

Reply via email to