Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:47046 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 69881 invoked from network); 24 Feb 2010 09:26:53 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 24 Feb 2010 09:26:53 -0000 Authentication-Results: pb1.pair.com header.from=ionut.g.stan@gmail.com; sender-id=pass; domainkeys=bad Authentication-Results: pb1.pair.com smtp.mail=ionut.g.stan@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.218.216 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: ionut.g.stan@gmail.com X-Host-Fingerprint: 209.85.218.216 mail-bw0-f216.google.com Received: from [209.85.218.216] ([209.85.218.216:41987] helo=mail-bw0-f216.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DA/F0-64178-BD0F48B4 for ; Wed, 24 Feb 2010 04:26:52 -0500 Received: by bwz8 with SMTP id 8so3532553bwz.23 for ; Wed, 24 Feb 2010 01:26:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=KKSp7WFQTTP9y/eTCUAwcyM/atQizuuETDMX6mZVO6M=; b=gRGDNONHqP6cSRGn2ZmzA91BL+O9adEriI/033LbgYG0WWox/fNojGrtxWx98Z3/Gy yxcXzyaLUBgXDgO0+itf4BFc0ZUV/AuTvAj0dWslVNwYANj2a5L7q7kwxiPgvrsgFUEp 5+YQvrz1AQ+L+eHOWSd5Tl0AD96AAXaatK4QM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; b=lE+2D3EIPO6Y1hf2wEn3NJSF5W9EAFDIKC/8J7kSjq4ws50IlPHSUC9HuBS/emELrI yiDPV6oWBFi7hzVE4mTeOMmV8eH1BLQ9ylkZZ3ziR2oGLMqYzhR31awCTNE99a1DFOXo toRgj3woiwiaJHRUkEvoef2PT1JFcuOOs9AGw= Received: by 10.204.34.81 with SMTP id k17mr3296309bkd.78.1267003604797; Wed, 24 Feb 2010 01:26:44 -0800 (PST) Received: from l.local ([82.208.177.66]) by mx.google.com with ESMTPS id 16sm2485819bwz.1.2010.02.24.01.26.43 (version=SSLv3 cipher=RC4-MD5); Wed, 24 Feb 2010 01:26:43 -0800 (PST) Message-ID: <4B84F14A.5050809@gmail.com> Date: Wed, 24 Feb 2010 11:28:42 +0200 User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.7) Gecko/20100111 Thunderbird/3.0.1 MIME-Version: 1.0 To: internals@lists.php.net References: <4B54FC87.8070106@zend.com> <4F.56.22457.408955B4@pb1.pair.com> <4B55D850.8000604@zend.com> <4B808294.1070801@keryx.se> <4B844DBA.4050804@zend.com> <4B845E0C.7040005@gmail.com> <4B84CD95.7030206@zend.com> In-Reply-To: <4B84CD95.7030206@zend.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] On closures and lamdba From: ionut.g.stan@gmail.com ("Ionut G. Stan") On 2/24/10 8:56 AM, Stanislav Malyshev wrote: > Hi! > >> Correct me if I'm wrong, but given the fact that PHP only* supports >> functions defined in the global space**, with the additional ability to >> import global variables using the global statement, wouldn't that make >> named functions able to close-over global variables? > > It's different mechanism. Globals are just references to variables in > same global scope, they are not bound to any function, etc. > >> And, if the above is true, wouldn't it be consistent to support the use >> statement on named functions, and then deprecate the global statement? > > That's why I'm not a big fan of that "consistency" argument here on the > list - because it usually means "let's implement some feature because I > like it", but not more. Which is fine, but it has nothing to do with > consistency. > Particularly, scope capturing and all that has very little relation to > what globals do. Closures carry around "captured" variables. Regular > functions don't. Global scope exists independently of them. > The mechanism is similar (well, it's the same engine, how many there can > be? :) but they are not the same. What I was thinking when writing my previous email was this: 1. Named functions using global variables (both can read and modify the global variable): $global_var = 'foo'; function named_1() { global $global_var; } function named_2() { global $global_var; } 2. Closures (both lambdas can read and modify the local var) list($closure_1, $closure_2) = call_user_func(function () { $local_var = 'foo'; return array( function () use(& $local_var) {}, function () use(& $local_var) {}, ); }); As I see it right now, there's a duplication of concepts in there. The only difference is that the first example uses the global scope, while the second one uses the scope of an anonymous function. But I admit this is just a nice-to-have thingy, not a necessity. Well, for me at least. So I won't ask for it anymore :). > >> ** namespaced functions don't make any difference, as variables aren't >> namespaced. > > I'm not sure what you mean here, as variables in PHP are scoped. Global > functions are not scoped, of course, but can be namespaced. I mean that variables declated outside of a namespaced function or class are not namespaced. namespace foo; $this_is_a_global_var = 'foo'; function namespaced_function() { global $this_is_a_globa_var; } -- Ionut G. Stan I'm under construction | http://blog.igstan.ro/