Ocramius / ProxyManager

🎩✨🌈 OOP Proxy wrappers/utilities - generates and manages proxies of your objects
MIT License
4.95k stars 188 forks source link

Replace initializer closures with anonymous classes + interfaces #334

Open Ocramius opened 8 years ago

Ocramius commented 8 years ago

Initializers are quick to write, but very complex to maintain and refactor, since no explicit contract is provided.

Luckily, since PHP 7, it is possible to simply use anonymous classes.

What we can do is providing a decent interface for initializers now:

interface GhostObjectInitializer
{
    public function __invoke(
        LazyLoadingGhostObject $proxy,
        ?GhostObjectInitializer $initializer,
        array $properties
    ) : bool;
}

Consumers can then use an anonymous class to instantiate such an initializer inline, but would still retain the BC and refactoring advantages of the stronger type hinting.

Ocramius commented 3 years ago

While this is still somewhat relevant, we now have detailed type signatures for initializers passed to proxy factories. For example:

https://github.com/Ocramius/ProxyManager/blob/f65ae0f9dcbdd9d6ad3abb721a9e09c3d7d868a4/src/ProxyManager/Factory/LazyLoadingGhostFactory.php#L78-L84

Such signatures already provide decent type-checking to the end-user.