Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:53547 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 95272 invoked from network); 23 Jun 2011 18:40:17 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Jun 2011 18:40:17 -0000 Authentication-Results: pb1.pair.com smtp.mail=martinscotta@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=martinscotta@gmail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.210.45 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: martinscotta@gmail.com X-Host-Fingerprint: 209.85.210.45 mail-pz0-f45.google.com Received: from [209.85.210.45] ([209.85.210.45:41444] helo=mail-pz0-f45.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id AE/13-53684-470630E4 for ; Thu, 23 Jun 2011 11:49:09 -0400 Received: by pzk30 with SMTP id 30so1557669pzk.18 for ; Thu, 23 Jun 2011 08:49:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=qFzBJrTp4L/5e4SBd53xfjTfgw8ZJnIqMsmgt3+N9lQ=; b=vpfirT73x9Gv4+Rq1djxUdmYYhU9yitvFuux0YdRCeJlzBFZDFECr/u+lqiMkzE794 9nlct9FgcR4ybYbY5a4ujpP+NVGtiYunPEYx7sUAW+TtQdpD4t7nEcULWkXvx8i3bZdZ sT4s06fCohx+M8Oz+x1XRwF+FS6g6DiM3VzJQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=HFDNZ5dNanHKD2wonew8fPlU8Gk6LKb2u4F0aS8oTMgGRdDh1nw7Dh3dIEMdW5k9fZ mrK4sKx4ioCv7ugJ5G/iT/qa6h+KglGXXae/169j3CGEfvJX7UMlw4PYFAWg2IqfB5zY U6sJlSy7O8aXlTDiTXMjFS/mx7qJ8IJVp9E6M= MIME-Version: 1.0 Received: by 10.68.63.233 with SMTP id j9mr1124720pbs.407.1308844145891; Thu, 23 Jun 2011 08:49:05 -0700 (PDT) Received: by 10.68.56.74 with HTTP; Thu, 23 Jun 2011 08:49:05 -0700 (PDT) In-Reply-To: <4E035B7E.4010703@php.net> References: <4E0355AE.4080305@php.net> <4E035B7E.4010703@php.net> Date: Thu, 23 Jun 2011 12:49:05 -0300 Message-ID: To: Stefan Neufeind Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary=bcaec539645a61da0304a66306ff Subject: Re: [PHP-DEV] Variable scopes for language constructs (foreach, ...) From: martinscotta@gmail.com (Martin Scotta) --bcaec539645a61da0304a66306ff Content-Type: text/plain; charset=ISO-8859-1 Martin Scotta On Thu, Jun 23, 2011 at 12:27 PM, Stefan Neufeind wrote: > On 06/23/2011 05:17 PM, Anthony Ferrara wrote: > > Personally, I don't care for the concept of a block scope. I do > > understand that it can have benefits and make certain tasks easier. > > But it can also lead to weird bugs and inconsistencies. For example, > > take the following code: > > > > $good = false; > > > > foreach ($array1 as $value) { > > $good = $good & $value; > > } > > > > unset($good); > > > > foreach ($array2 as $value) { > > $good = $value; > > } > > > > var_dump($good); > > > > What's the value of the dump? Should it be null? Should it be the > > last element of array2? Why? > > From my feeling it would be the present outside of the first > foreach()-loop right before the unset() since the variable was created > outside the foreach() already. Then it was unset (doesn't exist) and > recreated inside the second foreach-loop. > > If you even wanted to explicitly say that you don't want to go for the > outside-visibile $good then, as proposed, you could use some > > var $good; > > inside the foreach-loop imho to declare that you want a new variable > only visible inside the block. When that ends the outside-visibile $good > would be used again. > > In case of "clean programming" the $good before the first example would > be there anyhow if somebody wants to rely on $good having a value after > the first foreach(). > > > We now have the ability to close around scopes, so I fail to see the > > reason to do this. If you want a separate scope, then do an extract > > method on the loop. Pull it out to another method and be done. It'll > > be cleaner anyway. Besides, if you buy the arguments made in most > > clean code books, you shouldn't have more than 1 block in a single > > function anyway (as then it starts to do too much). So the scoping > > issue becomes pointless at that point. > > You can always argue that creating smaller methods (like "no methods > with more than 100 lines" or so) would limit the problems of a forgotten > (not unset()) reference-variable. But imho that's not the point. > > > Kind regards, > Stefan Neufeind > > > On Thu, Jun 23, 2011 at 11:09 AM, Ferenc Kovacs > wrote: > >> On Thu, Jun 23, 2011 at 5:03 PM, Stefan Neufeind > wrote: > >> > >>> Hi, > >>> > >>> I've lately discussed with a colleague which scopes of variables exist > >>> for PHP or would probably make sense. In general I think the general > >>> idea of having variables available all throughout a function is okay as > >>> this allows things like > >>> > >>> foreach($vals as $v) { > >>> // ... > >>> $found = true; > >>> } > >>> if($found) { > >>> // ... > >>> } > >>> > >>> (setting $found inside the loop while still being able to access it > >>> outside) > >>> > >>> But the interesting part is that $v is also still available outside the > >>> loop (last value). While most people would say this is not a big > >>> problem, it can become problematic when using references. > >>> > >>> foreach($vals as &$temp) { > >>> // ... > >>> } > >>> // ... > >>> $temp = 5; > >>> (when you don't think about the reference anymore but want some > >>> temp-variable) > >>> > >>> > >>> If this has been "throughly discussed" before, please excuse. But if > not > >>> maybe somebody could share his oppinion on the following proposal. > >>> > >>> What if we (for example with PHP 5.4 or if not possible maybe with the > >>> next one) change the behaviour so that > >>> > >>> * variables used for key/value in foreach (probably other places?) > would > >>> be limited to that loop-scope > >>> > >>> and maybe > >>> * variable $found in the first example would need to be initialised > >>> before the loop. Otherwise it would be a new variable inside the scope > >>> of foreach that would be gone afterwards > >>> > >>> and/or maybe > >>> * allowing to explicitly limit variable-scopes inside blocks, for > >>> example by allowing var $found somewhere inside a function to > allow > >>> things like > >>> > >>> if($a) { > >>> var $temp; > >>> > >>> $temp = 5; > >>> } > >>> // and $temp would be gone here; was limited to the scope in which it > >>> was defined by var > >>> > >>> > >>> Hope this is not too much of a non-sense idea to you :-) > >>> > >>> > >> Hi, > >> > >> it was discussed many times on the list, and this behavior is also > >> documented, see > >> http://php.net/manual/en/control-structures.foreach.php > >> > >> "Reference of a $value and the last array element remain even after the > >> foreach loop. It is recommended to destroy it by unset()." > >> > >> personally I find that weird, and unintuitive, but changin that in a > major > >> or minor version could be changed if we chose to. > >> > >> Tyrael > >> > > > > foreach ($values as $value) { // use $value } unset ($value); // <-- unset done inside core foreach ($values as $key => $value) { // use $key and $value } unset ($key, $value); // <-- unset done inside core will this just do the trick? > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --bcaec539645a61da0304a66306ff--