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]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to