Re: [PHP4BETA] Global variables inside a function?
| From: | Andreas Pour | Date: | Wed, 05 Apr 2000 22:09:23 +0000 |
| Subject: | Re: [PHP4BETA] Global variables inside a function? | ||
| References: | 1 2 3 4 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-13196@lists.php.net to get a copy of this message | ||
Hi,
Lars Torben Wilson wrote:
>
> Andreas Pour writes:
> > Lars Torben Wilson wrote:
> > >
> > > Peter Kovacs writes:
> > > > Is there an easy and efficient way to declare all the global variables to
> > > > be global within a function?
> > > >
> > > > Peter
> > > >
> > > > --------------------------------------------------
> > > > Peter D. Kovacs KnowPost.com LLC
> > > > Lead Engineer peter@knowpost.com
> > > > --------------------------------------------------
> > >
> > > In PHP4, this is simple: just put 'extract( $GLOBALS );' at the
> > > beginning of the function definition:
> >
> > Unfortunately, this only gives you read, not write, access to the
> > GLOBALS, and it's also not especially efficient. What would be nice is
> > a way to modify the scoping rules to be more C/C++/etc.-like, maybe w/ a
> > func like:
> >
> > set_var_scope("LOCAL_ONLY") // the default
> > and
> > set_var_scope("LOCAL_GLOBAL")
> >
> >
> > Ciao,
> >
> > Andreas
>
> True. Although you can get read/write access to all globals, it's even
> less efficient than the extract() option. The following is one way,
> which could even be easily extended to allow you to manage a global
> array of variable names to be included in the bulk globalization. That
> would be even worse for efficiency, though, and the code would quickly
> start looking really ugly. :)
>
> <?php
> $booger = 'This is a test';
>
> function foo() {
> while ( list( $key, $val ) = each( $GLOBALS ) ) {
> global $$key;
> }
> echo $booger;
> }
Yes, here function overhead becomes quite prohibitive :-).
An easier way to have a "function" that accesses all GLOBALS is to put
it by itself in a file and 'include()' it -- and also leave out the
function declaration part ;-).
>
> foo();
> ?>
>
> Otherwise, the scoping rules aren't that hard to get used to.
I politely disagree. I can't tell you how many times I've gotten "not
defined" errors . . . . Guess I'm an old dog when it comes to scoping
rules . . . .
> On large
> projects, it's nice to know that your locals are _always_ local unless
> you specifically globalize them.
I like the C++ approach the best -- if you throw a declaration in front
of it it's always local, and nicely enough you can include the
declaration in a statement. PHP also allows this, but only in class
definitions . . . . So the parser should be able to handle this already.
> It seems that being able to change
> that would result in increased ambiguity, and possibly problems with
> exchanging code with those whose existing code uses the opposite
> setting.
Hence my set_var_scope() suggestion -- this would operate on a
per-function level. If it's set to LOCAL_GLOBAL, if a symbol is not
found in the local symbol table, you just do a global lookup -- minimal
code required, unless Zend does something I am not anticipating.
>
> There are many who do not believe that the C-style scoping rules are a
> Good Thing, at least not for a higher-level language like PHP. Opinions,
> of course, vary. :)
That's why the configurable approach is so appealing :-).
Ciao,
Andreas