Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:53538 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 64508 invoked from network); 23 Jun 2011 15:19:03 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Jun 2011 15:19:03 -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.42 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.42 mail-pz0-f42.google.com Received: from [209.85.210.42] ([209.85.210.42:44367] helo=mail-pz0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 17/22-53684-669530E4 for ; Thu, 23 Jun 2011 11:19:03 -0400 Received: by pzk37 with SMTP id 37so1372055pzk.29 for ; Thu, 23 Jun 2011 08:18:59 -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=QlqIsut8V5+T/KfDl41uk5A7NOYf2bF8GTOadqPpYWc=; b=r9suAukPVgZwN7+iQ9siqYmiGDy3vAiTHfYUHK38xIW385UUiRjk+BR2on38yGPooU wgwLzfSEVJAriw6hhGLOKAPAKokGnU9au5v13wO5TtXyWtJ37y493h6qWnyjo2ZNptGw +Waod8K7h+qd819WIKhubyV2Mu0xxE6PUTm8Q= 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=RYPcvUvd9HJT6bwVzLmYYN+nFgmatthC8ajj81XncLHmjsR7fNUlCJc1GFfQYZX8SU B44MueiUsA68YthGtrrMO6Lxw7dmd7Rdjed5HSGnyhqYoG8cRnGdEOr9xA0SaC1DAU13 cB4aNb+0lthuNK4qguDJQV8EI5+zsoMmc9Jw0= MIME-Version: 1.0 Received: by 10.68.16.169 with SMTP id h9mr1218358pbd.206.1308842339505; Thu, 23 Jun 2011 08:18:59 -0700 (PDT) Received: by 10.68.56.74 with HTTP; Thu, 23 Jun 2011 08:18:59 -0700 (PDT) In-Reply-To: References: <4E0355AE.4080305@php.net> Date: Thu, 23 Jun 2011 12:18:59 -0300 Message-ID: To: Paul Dragoonis Cc: Ferenc Kovacs , Stefan Neufeind , internals@lists.php.net Content-Type: multipart/alternative; boundary=bcaec51f8fb1b694e904a6629a57 Subject: Re: [PHP-DEV] Variable scopes for language constructs (foreach, ...) From: martinscotta@gmail.com (Martin Scotta) --bcaec51f8fb1b694e904a6629a57 Content-Type: text/plain; charset=ISO-8859-1 Martin Scotta On Thu, Jun 23, 2011 at 12:12 PM, Paul Dragoonis wrote: > On Thu, Jun 23, 2011 at 4:09 PM, 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. > > > > > I would like to see, in a future version, the local var of the foreach()'s > refcount being dropped to 0 or something similar. Unless it is a > by-reference variable in which case it should remain untouched. > > > > Tyrael > > > Also I've love to be able to.... $var = 'global'; class Foo { private $var = 'member'; function test1() { return $var; } function test2($var='param') { return $var; } function test3() { $var = 'variable'; return $var; } function test4() { global $var; return $var; } } $foo = new Foo(); var_dump( $foo->test1() ); // member var_dump( $foo->test2() ); // param var_dump( $foo->test3() ); // variable var_dump( $foo->test4() ); // global From a "language" POV this seems very possible. Is it possible from a core POV ? Do you think this need a RFC? I can write some --bcaec51f8fb1b694e904a6629a57--