Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:28399 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 51011 invoked by uid 1010); 18 Mar 2007 23:41:27 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 50996 invoked from network); 18 Mar 2007 23:41:27 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Mar 2007 23:41:27 -0000 Authentication-Results: pb1.pair.com smtp.mail=wez@omniti.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=wez@omniti.com; sender-id=pass; domainkeys=good Received-SPF: pass (pb1.pair.com: domain omniti.com designates 66.225.209.50 as permitted sender) DomainKey-Status: good X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: wez@omniti.com X-Host-Fingerprint: 66.225.209.50 mail.omniti.com Linux 2.5 (sometimes 2.4) (4) Received: from [66.225.209.50] ([66.225.209.50:34209] helo=mail.omniti.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 9F/B5-18356-42ECDF54 for ; Sun, 18 Mar 2007 18:41:26 -0500 X-DKIM: Ecelerity dkim_sign implementing draft-ietf-dkim-base-00 DKIM-Signature: a=rsa-sha1; d=omniti.com; s=test; t=1174261281; c=simple/simple; q=dns; i=@omniti.com; h=From; b=idlnCnwwWGZe3756dLOtmg9wNRoBPYNSmo/RWYC9srkzRqXFLhIMw8ua6JYZ65xY SdQEH0l5LRUe9/RgIySWRcC5ZgH5QK1J9JIqTbioH1GC3ldjqAjax/t4fI3oUh/W X-DomainKeys: Ecelerity dk_sign implementing draft-delany-domainkeys-base-01 DomainKey-Signature: q=dns; a=rsa-sha1; c=nofws; s=test; d=omniti.com; h=Authentication-Results:Received:Mime-Version:Content-Type:Message-Id:Content-Transfer-Encoding:From:Subject:Date:To:X-Mailer; b=N12eFJGxDmecSlJyhwu8T5bUtRPlWAlwGxE+eV3K62VgP+Wl1qVcBY/g6/pGr+nq vcg4KEOMvqSHKyiVEFti56KP8DQttrBuMpYVcLRxsSVO8P+yIiiF8f6TCY9rSM4V Authentication-Results: mail.omniti.com smtp.user=wez; auth=pass (LOGIN) Received: from [76.100.30.170] ([76.100.30.170:54447] helo=[192.168.50.4]) by mail.omniti.com (ecelerity 2.1.1.12 r(14453)) with ESMTPSA (cipher=AES128-SHA) id 1C/EC-26454-D1ECDF54 for ; Sun, 18 Mar 2007 19:41:21 -0400 Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-ID: <86478A67-DCA2-4000-9EF0-DA4338E8389B@omniti.com> Content-Transfer-Encoding: 7bit Date: Sun, 18 Mar 2007 19:41:16 -0400 To: internals@lists.php.net X-Mailer: Apple Mail (2.752.3) Subject: PATCH: anonymous functions in PHP From: wez@omniti.com (Wez Furlong) We've been daydreaming about the ability to do something like this in PHP: $data = array("zoo", "orange", "car", "lemon", "apple"); usort($data, function($a, $b) { return strcmp($a, $b); }); var_dump($data); # data is sorted alphabetically In the past, people have been told to use the travesty that is create_function() to emulate this kind of behavior, well, it turns out to be a minor patch to the parser to pull that into the core. You can find my prototype patch at http://pastebin.ca/400871 (against PHP_5_2) The way it works is by making the expression: function() {} evaluate to the (generated) name of the anonymous function. $foo = function() {}; sets $foo to a string like "__zend_anon_1", which can then be passed around as a callback name, just like the way that create_function() works, except that you don't need to use crazy quoting to declare any kind of moderately complex function. There's one minor flaw in my implementation for ZTS enabled systems (just need to move the anon function counter into CG() to solve that. So, the question is, do we want this in PHP? --Wez.