Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:71670 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 95198 invoked from network); 28 Jan 2014 13:36:38 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 28 Jan 2014 13:36:38 -0000 Authentication-Results: pb1.pair.com smtp.mail=happy.melon.wiki@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=happy.melon.wiki@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.178 as permitted sender) X-PHP-List-Original-Sender: happy.melon.wiki@gmail.com X-Host-Fingerprint: 209.85.216.178 mail-qc0-f178.google.com Received: from [209.85.216.178] ([209.85.216.178:48326] helo=mail-qc0-f178.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 53/CE-01140-562B7E25 for ; Tue, 28 Jan 2014 08:36:38 -0500 Received: by mail-qc0-f178.google.com with SMTP id m20so514906qcx.9 for ; Tue, 28 Jan 2014 05:36:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:content-type; bh=gFggiztbjLKpjPS8sce2BSdyG7KnCxY/Krdj97wv9NM=; b=rGj3Jc/jMG3N7E0FfjXydCQGFH100+oYEvG/ZJfSQrankaxtolSS+YbLxeO6GDxPNq KXA2R9v6H+H9rrLu2Q5msM6hYCToALlzjJTtFdHIcJc++Fubdme/VblNA+tUM8HuA1yi XLX612K4hsaaLnOxfgiSmdhd/dHzx7gZWZ0MzzqfwRozsQRsr2vdQsuS0R3NQJ19OwEx kqSO6PdeP828RJ7cqR/ENtDm+h5ykc/YCmO6ct6SMRFzvtkwn3nS/U3dttm1v8BgzQku DifTwbOhYQ8yiXkCZ0ZgTqnl67tP4ql138831irFIy9tAmllCkA3kYwDZLnkLVkkrfIE OqCA== MIME-Version: 1.0 X-Received: by 10.140.84.19 with SMTP id k19mr2196023qgd.98.1390916195013; Tue, 28 Jan 2014 05:36:35 -0800 (PST) Sender: happy.melon.wiki@gmail.com Received: by 10.140.86.72 with HTTP; Tue, 28 Jan 2014 05:36:34 -0800 (PST) In-Reply-To: References: <1796143171.126442.1390903281925.open-xchange@app2.ox.registrar-servers.com> Date: Tue, 28 Jan 2014 13:36:34 +0000 X-Google-Sender-Auth: D9CONPBZtauroMMcc_rv044EBv4 Message-ID: To: PHP internals list Content-Type: multipart/alternative; boundary=001a11c12e4cb756d004f107e9ce Subject: Re: [PHP-DEV] Lexical scoping From: happy.melon.wiki+gb@gmail.com (George Bond) --001a11c12e4cb756d004f107e9ce Content-Type: text/plain; charset=ISO-8859-1 On 28 January 2014 13:25, Lazare Inepologlou wrote: > 2014-01-28 Sebastian Krebs > > > 2014-01-28 Andrea Faulds > > > > > Good morning, > > > > > > Perhaps a future version of PHP (PHP 6? 5.7?) could have lexical > scoping > > > with a > > > "let" keyword? At the very least, it would make foreach() with a > > reference > > > less > > > of a problem: > > > > > > foreach ($array as let &$a) { > > > $a = 3; > > > } > > > // $a is not in this scope - no need to worry about accidentally > > > changing > > > last array item > > > > > > Any thoughts? > > > > > > > Hi, > > > > My 2 cents: That is a very minor improvement at the cost of an additional > > keyword. It is only to avoid, that you accidentally interfere with outers > > scope variables? If it is hard to track these variables, maybe your > > method/function is too big. > > > > > I agree. > > > > Regarding your example: > > > > foreach ($array as &$a) { > > $a = 3; > > } > > $a = null; // $a not a reference anymore. Problem solved :) > > > > > Problem not solved. You have just changed the last element of $array... > This is the exact problem that was to be avoided. > > AFAIK, this is a problem which is quite limited to foreach() (are there other examples?), so one (BC-breaking-but-probably-for-the-best) solution would simply be to unset the variable automatically if it wasn't already defined: foreach( $array as &$a ){ $a = 3; } var_dump( $a ); // E_NOTICE, $a is now undefined $b = 2; foreach( $array as &$b ){ $b = 3; } var_dump( $b ); // 3, $b was defined before we started the foreach, so it's not wiped Alternatively, you could say that the ultimate problem is that we don't have strong scoping - braces only define flow control, not scope. So you could introduce a strong-scoping structure. Maybe a double brace? foreach( $array as &$a ){{ $a = 3; }} var_dump( $a ); // E_NOTICE, $a is now undefined You could even say that an isolated brace pair (ie not one following a control statement) *always* produces a scope. --GB --001a11c12e4cb756d004f107e9ce--