Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:92469 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 83644 invoked from network); 19 Apr 2016 15:21:19 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Apr 2016 15:21:19 -0000 Authentication-Results: pb1.pair.com header.from=larry@garfieldtech.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=larry@garfieldtech.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain garfieldtech.com from 66.111.4.26 cause and error) X-PHP-List-Original-Sender: larry@garfieldtech.com X-Host-Fingerprint: 66.111.4.26 out2-smtp.messagingengine.com Received: from [66.111.4.26] ([66.111.4.26:50089] helo=out2-smtp.messagingengine.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 7A/B6-43496-CEC46175 for ; Tue, 19 Apr 2016 11:21:16 -0400 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 26CC120AFB for ; Tue, 19 Apr 2016 11:21:14 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute1.internal (MEProxy); Tue, 19 Apr 2016 11:21:14 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=XOyK/UyZeAlQ6Sx gI5+lyC78c4I=; b=UrtWbl6S6KX9L7V/aY6BndtMRpFgatepqUR874nn1T0nNgd kU5rVy71vj/Q9jniA0Bv/oCvxfw6+EKwNmZtdOb5srzYkojfyH+2ie+rCLVsc9vE vfaDPTSaSdTpsWu1vJO3qLhuN6tOEq4kqQ4MDbSHAECYzA2U54/YgwLqFhx8= X-Sasl-enc: jpxHvrmMhJ6xdURkHgXpjqOP7HCiB7txb7+tWeq10bcG 1461079273 Received: from Crells-MacBook-Pro.local (unknown [63.250.249.138]) by mail.messagingengine.com (Postfix) with ESMTPA id 59EB8C00018 for ; Tue, 19 Apr 2016 11:21:13 -0400 (EDT) To: internals@lists.php.net References: <5716310B.4060104@zend.com> <57163CB0.70608@zend.com> <5716410C.1080904@gmail.com> <57164550.7070008@zend.com> <57164889.1080609@gmail.com> Message-ID: <57164CE8.4090307@garfieldtech.com> Date: Tue, 19 Apr 2016 10:21:12 -0500 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Thunderbird/38.0.1 MIME-Version: 1.0 In-Reply-To: <57164889.1080609@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] RFC: Functional Interfaces From: larry@garfieldtech.com (Larry Garfield) On 4/19/16 10:02 AM, Rowan Collins wrote: > 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, 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 (My apologies to the authors if they aren't done with it; it's on the wiki so I'm assuming it's safe to mention in public.) That allows just the callable-type syntax (which I presume is what you're referencing), but you can then pass an object in using []-callable syntax: [$handler. 'handleUserEvent'], and then registerCallback() doesn't care if it was a function, method, closure, or whatever else. It can just () it. -- --Larry Garfield