Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:63392 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 65983 invoked from network); 13 Oct 2012 11:36:09 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Oct 2012 11:36:09 -0000 Authentication-Results: pb1.pair.com header.from=christian.kaps@mohiva.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=christian.kaps@mohiva.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mohiva.com from 80.67.29.7 cause and error) X-PHP-List-Original-Sender: christian.kaps@mohiva.com X-Host-Fingerprint: 80.67.29.7 smtprelay03.ispgateway.de Linux 2.6 Received: from [80.67.29.7] ([80.67.29.7:47303] helo=smtprelay03.ispgateway.de) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 47/4B-06472-82259705 for ; Sat, 13 Oct 2012 07:36:09 -0400 Received: from [87.181.60.248] (helo=[192.168.0.3]) by smtprelay03.ispgateway.de with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.68) (envelope-from ) id 1TN00i-000801-7k for internals@lists.php.net; Sat, 13 Oct 2012 13:36:04 +0200 Message-ID: <50795223.9090707@mohiva.com> Date: Sat, 13 Oct 2012 13:36:03 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120916 Thunderbird/15.0.1 MIME-Version: 1.0 To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Df-Sender: Y2hyaXN0aWFuLmthcHNAbW9oaXZhLmNvbQ== Subject: Re: [PHP-DEV] Closures and type hinting From: christian.kaps@mohiva.com (Christian Kaps) Am 12.10.2012 23:06, schrieb 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 > What did you mean with strict function signatures? Is this not a strict function signature? function(Foo $foo) {} Christian