Re: [RFC] Lazy Objects

From: Date: Wed, 24 Jul 2024 19:43:51 +0000
Subject: Re: [RFC] Lazy Objects
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message


On Wed, Jul 24, 2024, at 16:28, Nicolas Grekas wrote:
> 
> To Rob: proxying by interface can be implemented using regular code generation so it's not
> a blocker. Symfony does it already, and will continue to do it to cover the use case.

I'm not sure what you mean, as the RFC makes it look this should be completely legal, until you
get to the end:

interface Fancy {
  function sayHi(): void;
}
class A implements Fancy {
  function sayHi(): void { echo "hi!"; }
}

$container->register(proxyType: Fancy::class, factory: fn() => new A());

// Now container only knows that it needs to return a FancyClass and this is "legal
looking":

$lazy = new ReflectionClass($proxyType)->newLazyProxy($factory);
// same as
$lazy = new ReflectionClass(Fancy::class)->newLazyProxy(fn() => new A());

This ^ compiles, it's perfectly legal and makes 100% sense that I should be able to call
$lazy->sayHi() and it calls A::sayHi(), which is what you would expect. Instead, this is runtime
error.

— Rob


Thread (95 messages)

« previous php.internals (#124580) next »