Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:63368 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 42735 invoked from network); 12 Oct 2012 08:59:01 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Oct 2012 08:59:01 -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.18.44 cause and error) X-PHP-List-Original-Sender: christian.kaps@mohiva.com X-Host-Fingerprint: 80.67.18.44 smtprelay02.ispgateway.de Linux 2.6 Received: from [80.67.18.44] ([80.67.18.44:34401] helo=smtprelay02.ispgateway.de) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6B/F6-06472-3DBD7705 for ; Fri, 12 Oct 2012 04:59:00 -0400 Received: from [80.67.16.112] (helo=webmail.df.eu) by smtprelay02.ispgateway.de with esmtpa (Exim 4.68) (envelope-from ) id 1TMb56-0005y7-2i for internals@lists.php.net; Fri, 12 Oct 2012 10:58:56 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Date: Fri, 12 Oct 2012 10:58:56 +0200 To: Message-ID: X-Sender: christian.kaps@mohiva.com User-Agent: Roundcube Webmail/0.8.1 X-Df-Sender: Y2hyaXN0aWFuLmthcHNAbW9oaXZhLmNvbQ== Subject: Closures and type hinting From: christian.kaps@mohiva.com (Christian Kaps) 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?