Re: RFC: Functional Interfaces

From: Date: Tue, 19 Apr 2016 15:02:33 +0000
Subject: Re: RFC: Functional Interfaces
References: 1 2 3 4 5 6 7 8 9  Groups: php.internals 
Request: Send a blank email to internals+get-92468@lists.php.net to get a copy of this message
Dmitry Stogov wrote on 19/04/2016 15:48:
callable-type is much simpler solution for this use-case. Usage of Interfaces to check function prototypes is a bit tricky, but yes, this is a possible use-case. Do you like to work with framework, that use this trick for every callback?
If you don't use __invoke as the function name, you can have a single class implement multiple contracts, e.g. to register for more than one event type with different signatures. I guess if you had callable-types *and* union types, you could get the best of both worlds: interface UserEventCallbackInterface {
    public function handleUserEvent(UserEvent $event): boolean;
} public function registerCallback(EventCallbackInterface|callable(Event): boolean $handler) { ... } The implementation has to be a bit fiddlier here, because you need to sometimes call $handler->handleUserEvent($event) and sometimes $handler($event); with a functional interface, the engine makes $handler->handleUserEvent($event) work for you. The cost is that the call site has to be a bit more verbose to specify the interface, rather than implicitly meeting the contract. Regards, -- Rowan Collins [IMSoP]

« previous php.internals (#92468) next »