On Sun, Mar 29, 2026 at 4:44 PM Osama Aldemeery <[email protected]> wrote:
> Hi all, > > I'd like to propose an Invokable interface, the Stringable equivalent for > __invoke(). > > The idea has come up a few times over the years (most recently in the PR > #15492 discussion, where Gina suggested this exact approach) but never had > a concrete implementation. > > I've put one together: https://github.com/php/php-src/pull/21574 > > It follows the Stringable pattern: auto-implemented for any class defining > __invoke(), explicitly implementable with enforcement, and covariant to > callable in return type checks. > > I honestly don't feel there's a need for this, as we already have the callable type. It shouldn't matter if the callable is an object defining `__invoke()`, a function, a closure, or a first class callable. If you need to restrict to an object that's invokable, you can do that already via (object&callable). Further, PHPStan, Psalm, PHPCS, and php-cs-fixer all understand annotations around the callable type already to further define the allowed signature (e.g., number and types of arguments, return value). This is a solved problem already. Further, creating an interface for this is hugely difficult, if not impossible, due to the return value associated with the interface, as implementations will inevitably conflict with the Liskov Substitution Principle. If we say "void", you cannot declare an implementation with a return value. If you define mixed, you cannot have a void or never return. -- Matthew Weier O'Phinney [email protected] https://mwop.net/ he/him
