Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:47009 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 16630 invoked from network); 21 Feb 2010 02:54:58 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Feb 2010 02:54:58 -0000 Authentication-Results: pb1.pair.com header.from=joey@joeysmith.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=joey@joeysmith.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain joeysmith.com designates 209.90.98.146 as permitted sender) X-PHP-List-Original-Sender: joey@joeysmith.com X-Host-Fingerprint: 209.90.98.146 host-3.pl1071314.fiber.net Received: from [209.90.98.146] ([209.90.98.146:33219] helo=joeysmith.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6B/4C-29373-080A08B4 for ; Sat, 20 Feb 2010 21:54:57 -0500 Received: from joey by joeysmith.com with local (Exim 4.69) (envelope-from ) id 1Nj1yQ-0002hd-Gf; Sat, 20 Feb 2010 19:55:10 -0700 Date: Sat, 20 Feb 2010 19:55:10 -0700 To: Keryx Web Cc: internals@lists.php.net Message-ID: <20100221025510.GA9984@joeysmith.com> References: <4B54FC87.8070106@zend.com> <4F.56.22457.408955B4@pb1.pair.com> <4B55D850.8000604@zend.com> <4B808294.1070801@keryx.se> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4B808294.1070801@keryx.se> User-Agent: Mutt/1.5.18 (2008-05-17) Subject: Re: [PHP-DEV] On closures and lamdba From: joey@joeysmith.com (Joey Smith) > Oh, yes, the question: > > Wouldn't you agree that it is better for PHP to use the word closure as > it is being used in the JavaScript community? There's a pretty big difference between how PHP implements closures and how JavaScript implements them - in PHP, you have to explicitly request which variables will be bound to the closure. Here's an example I've been using on Freenode's ##PHP to demonstrate how a PHP closure might work. ================= $array = array( array('foo' => 17, 'bar'=>'last'), array('foo' => 1, 'bar'=>'first'), array('foo' => 3, 'bar'=>'middle'), ); function buildSorter($key) { return function ($a, $b) use ($key) { if ($a[$key] == $b[$key]) return 0; return ($a[$key] < $b[$key]) ? -1:1; }; } $sort = buildSorter('foo'); usort($array, $sort); var_dump($array); ================= Not an example that's going to rock anyone's world, obviously, but I think it's illustrative. However, because of an implementation detail (all anonymous functions are now implemented as instance of class 'Closure'), it might be hard to enforce a strict use of the word 'closure' here - although, it *is* kind of interesting to look at a var_dump() of $sort: object(Closure)#1 (2) { ["static"]=> array(1) { ["key"]=> string(3) "foo" } ["parameter"]=> array(2) { ["$a"]=> string(10) "" ["$b"]=> string(10) "" } } Unfortunately, we're not allowed to query for that 'static' property (Catchable fatal error: Closure object cannot have properties) and are are explicitly discouraged from relying on this detail of implementation ("Anonymous functions are currently implemented using the Closure class. This is an implementation detail and should not be relied upon.") So to answer your question, for my money, since I can't currently tell the difference in a programmatic way between an 'anonymous function' and a full 'closure', I don't find it that worrisome that the PHP world somewhat conflates the two terms.