Re: RFC: Functional Interfaces

From: Date: Tue, 19 Apr 2016 15:33:18 +0000
Subject: Re: RFC: Functional Interfaces
References: 1 2 3 4 5 6 7 8 9 10 11  Groups: php.internals 
Request: Send a blank email to internals+get-92471@lists.php.net to get a copy of this message
Larry Garfield wrote on 19/04/2016 16:21:
Which is why I'm not sure I like that approach, because the place I then call $handler needs to have a conditional in it. There's another RFC that was posted to Twitter but hasn't made it to the list yet that seems to solve this better: https://wiki.php.net/rfc/callable-types
I'm not sure which approach you mean, but in Joe's proposal, you always call $handler as though it was an object implementing the interface; have a look at the ILog example in the RFC. With a typed callable, you can always invoke as $handler(...) will work, but the implementer can't create an object that meets more than one contract, because they can only provide one __invoke method. interface UserEventCallbackInterface {
    public function handleUserEvent(UserEvent $event): boolean;
} interface CommentEventCallbackInterface {
    public function handleCommentEvent(CommentEvent $event): boolean;
} class Logger implements UserEventCallbackInterface, CommentEventCallbackInterface { ... } $logger = new Logger; $simple_user_handler = function(UserEvent $event) implements UserEventCallbackInterface: boolean { ... } In Joe's proposal, $logger and $simple_user_handler can be used interchangeably by calling $handler->handleUserEvent(...); the receiving code doesn't need to do anything special to accept the closure form, because the engine transforms it into the required object. Regards, -- Rowan Collins [IMSoP]

« previous php.internals (#92471) next »