Re: [RFC] Import of variables

From: Date: Sun, 09 Aug 2020 11:38:03 +0000
Subject: Re: [RFC] Import of variables
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-111388@lists.php.net to get a copy of this message
On 09/08/2020 10:01, Manuel Canga wrote:
Hi Internals, I'd like to open up a discussion around the implementation of a new functionality: 'import of variables'. This functionality would allow to create a new 'use vars' keyword in order to can use( or cannot use ) global variables in local scope( of current file ).
Right now, PHP has no notion of "file scope", and making that the _default_ would be a huge compatibility break. The "use vars none" variant that opts _out_ of the global scope might be possible, but at that point, you can just use the common JS trick of wrapping your code in a function and immediately running it: <?php (function() { global $a, $c, $d; echo $a; //1 echo $b; //Warning: undefined var $b $c = 'a value'; //replace value in global var })(); The last example, where the function uses "global" but only gets the globals if they're also "used" in the current file, seems like it would make more sense using a class, a static method, and some private static variables, since right now we don't think of a function as "belonging to" a file in the same way it would belong to a class. Regards, -- Rowan Tommins (né Collins) [IMSoP]

« previous php.internals (#111388) next »