Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:63381 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 11356 invoked from network); 12 Oct 2012 21:06:28 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Oct 2012 21:06:28 -0000 Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.42 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.212.42 mail-vb0-f42.google.com Received: from [209.85.212.42] ([209.85.212.42:39041] helo=mail-vb0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 64/D3-06472-45688705 for ; Fri, 12 Oct 2012 17:06:28 -0400 Received: by mail-vb0-f42.google.com with SMTP id fs19so3745711vbb.29 for ; Fri, 12 Oct 2012 14:06:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=SbU/JTJ0SkyWWDeRo/SYPWd0BDGAhwa6W0BoiBrxWmk=; b=YINg9nRwJrU+k5GTKDNV/VCgrwnpTdluELHgEF9QPm1MwiMCqefusj4Di4RtlASIx/ rr8HFse6BjEhzEqHaFv03sGqhIZf6EUsPTBdK8paiTRhmKrZm99RtKycjtHqoijaYRDP ovunnuqQeGeN9h6CTJV3e+Pxi51fiBkt1M1Wu0bahLpV0QJerAmfuPkOz8w5nU1RdrjX z0L67BP5dc/mQZyvkz30TCPYPIOmqu17ae6uLXzhnWAsDALaKksb9W42JclgyT3M5NrD wlSqLU5wL09HBO12E04t3xV40Rc5UaUmmgabpeJRwX3iw14zBNjFCP71Sv1IMyE/yv7Y CgJg== MIME-Version: 1.0 Received: by 10.220.223.3 with SMTP id ii3mr3121964vcb.74.1350075985508; Fri, 12 Oct 2012 14:06:25 -0700 (PDT) Received: by 10.58.161.70 with HTTP; Fri, 12 Oct 2012 14:06:24 -0700 (PDT) In-Reply-To: References: Date: Fri, 12 Oct 2012 23:06:24 +0200 Message-ID: To: Christian Kaps Cc: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] Closures and type hinting From: nikita.ppv@gmail.com (Nikita Popov) On Fri, Oct 12, 2012 at 10:58 AM, Christian Kaps wrote: > At the moment it isn't possible to restrict/define the arguments for a > closure which is defined as parameter for a method. Please look at this > small example. > > interface Broker { > > public function scan(Request $request, Closure $onFound); > } > > An implementation of the interface could be as follows: > > class BrokerImpl implements Broker { > > /** > * @var Service > */ > private $service = null; > > public function scan(Request $request, Closure $onFound) { > > if ($request->contains('some value')) { > $onFound($this->service); > } > } > } > > The problem is that I can pass every closure to the scan method. > > $broker = new BrokerImpl(); > $broker->scan($request, function() { /* do something */ }); > > Sometimes I would like to restrict the closure passed to this method. So > that only closures of a certain type which has an argument "Service > $service" could be passed to this method. > > $broker = new BrokerImpl(); > $broker->scan($request, function(Service $service) { /* do something */ }); > > Would it not be possible to extend the Closure class and then define an > abstract method signature for the __invoke method. > > class OnFoundClosure extends Closure { > > public abstract function __invoke(Service $service); > } > > And then you can define the interface as follows: > interface Broker { > > public function scan(Request $request, OnFoundClosure $onFound); > } > > Now if you pass a closure to the scan method which doesn't follow the > signature of the __invoke method, the engine should throw an error. > > What do you think? Please also don't forget that PHP doesn't have strict function signatures. E.g. you can write function($foo) { echo $foo; } but you could also write function() { $foo = func_get_arg(0); echo $foo; } So I'm not sure just how much sense function signature checking makes in PHP ;) Nikita