Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:63369 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 44573 invoked from network); 12 Oct 2012 09:18:46 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Oct 2012 09:18:46 -0000 Authentication-Results: pb1.pair.com smtp.mail=ircmaxell@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ircmaxell@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.42 as permitted sender) X-PHP-List-Original-Sender: ircmaxell@gmail.com X-Host-Fingerprint: 209.85.215.42 mail-la0-f42.google.com Received: from [209.85.215.42] ([209.85.215.42:60394] helo=mail-la0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 98/57-06472-570E7705 for ; Fri, 12 Oct 2012 05:18:45 -0400 Received: by mail-la0-f42.google.com with SMTP id e6so1875042lah.29 for ; Fri, 12 Oct 2012 02:18:42 -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=0kFbhIMyT/FQFZldcWDBipdrwiD2A6329w3KwU434KY=; b=RjqAUODuHbgwk+gEh4A/kp5H53QVQrb7/ODLG9A2GtnI42AiMkTZHoDFHmGQ50c9Av tNKppK8fyXxiHivQ989eTziFN9oCX4cEqaW+av59eDfZNUOqXX5ZbSJOycIR3rc9nI93 rmhJahjavzqEjBdD/7TriYnCc3rPER3cXUbY+GumRTbOjKmdgwuuw3cCGOVsLiKXURwU UIzQCkcLqChevgcVa6M3f9EFCQBsz+NNUfnYnz5KpH8WH0Bt8Ih6vuzbA6Xe+QNqLdTB qSFC3QkUhubDK3lnCJ9NOJoWJtYlyDQi5BKSBOn6flOISMc7cZivmxpYgZgwiZ61QVQB h5BQ== MIME-Version: 1.0 Received: by 10.112.13.173 with SMTP id i13mr1430533lbc.108.1350033522511; Fri, 12 Oct 2012 02:18:42 -0700 (PDT) Received: by 10.114.22.1 with HTTP; Fri, 12 Oct 2012 02:18:42 -0700 (PDT) Received: by 10.114.22.1 with HTTP; Fri, 12 Oct 2012 02:18:42 -0700 (PDT) In-Reply-To: References: Date: Fri, 12 Oct 2012 05:18:42 -0400 Message-ID: To: Christian Kaps Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary=f46d0401f77f8b50eb04cbd92c68 Subject: Re: [PHP-DEV] Closures and type hinting From: ircmaxell@gmail.com (Anthony Ferrara) --f46d0401f77f8b50eb04cbd92c68 Content-Type: text/plain; charset=ISO-8859-1 On Oct 12, 2012 10:59 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? > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > Honestly, I would suggest using a class directly if you want that kind of type control. Closures are meant to be a conscience, not the end all be all implementation. I understand what you are looking for. But to me, unless we implemented function pointers (typeable callbacks), I can't see the generalized use case. At least that isn't already solvable using classes, interfaces and polymorphism... Anthony --f46d0401f77f8b50eb04cbd92c68--