Re: Good way to make variables global (?)

From: Date: Tue, 10 Oct 2000 01:04:22 +0000
Subject: Re: Good way to make variables global (?)
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-19272@lists.php.net to get a copy of this message
Good call, Lars! To avoid flames of "don't use global variables, just pass variable names to the function" I'd like to take a minute to explain why I wanted to implement global variables this way. I am making a web application. I have one main function that acts as a template (which is basically a set of tables.) This function calls a function called text_include that prints different pieces of text depending on an action code that gets passed around at the end of the script URL. Each page of this application references a database. There are several different forms to add, delete, and update data in the database. Each of these forms is passed via POST, which makes a variable name called, for example, $add_user or $update_user. I made each form have different variables depending on what the form did so I wouldn't lose track of my variables. The real problem comes in when these forms are POSTed. The function text_include does NOT automatically pick up the $HTTP_POST_VARS array as global when I try to call it via, say, $add_user. I thus have a few choices, the most viable of which is to either pass each variable as global independently as a GLOBAL declaration at the beginning of the function, or loop through $HTTP_POST_VARS when the form is parsed by the text_include function. I can either declare about 40 variables global one at a time (what a pain!) or loop through and extract $HTTP_POST_VARS each time I parse a form. This second option is okay; however, since each of my forms have separate variable names, I have elected to just write the extract code once and loop through the $GLOBALS array. This is obviously not the right choice all the time. Had my application consisted of one form, I would have elected to use the extract function on $HTTP_POST_VARS. However, since my application consists of a large amount of forms, I saved some cutting-and-pasting and just decided to make sure I wasn't overwriting any necessary variables. (I also had other variables, such as $action, that needed to be global.) Since my app reads and writes from a database, variable names aren't really that important once they have been written/read from to the database, which is the other reason I elected to do it this way... and this way, if I decide to add more functions later, I don't have to worry about why they are not receiving the necessary data. Erica At 05:32 PM 10/9/2000 -0700, Lars Torben Wilson wrote:
Erica Douglass writes: I recently stumbled upon a way to make all variables global. I did this because I got sick of writing out global variable after global variable in my functions. It seems to work just great in my script, but I wanted to read others' input on this. The advantages are obvious: no huge GLOBAL declarations. However, I wanted to know if there are any hidden pitfalls that I haven't noticed (besides the fact that a global variable could be easily overwritten.) Here's the code: function example_function () { reset ($GLOBALS); while ( list($key, $val) = each($GLOBALS) ) { extract ($GLOBALS); } My impression: This is a very cool snippet of code... as long as you understand the implications. Erica Here's the first thing I notice: the above code extracts _all_ elements of $GLOBALS, for _each_ element of $GLOBALS. In other words, extract() already extracts() all of the elements of the array it is given; you don't need to do it once for each element. So if your $GLOBALS array has, say, 20 elements, the above code is doing 20 times the work it needs to do. Try this: function example_function() {
    extract($GLOBALS);
    . . .
} Or for those still running PHP 3: function example_function() {
    reset($GLOBALS);
    while (list($key) = each($GLOBALS)) {
        if ($key != 'GLOBALS') {   // Just to suppress a warning
            global $$key;          // This is the important bit
        }
    }
    . . .
} ObDisclaimer: :) Personally, however, I wouldn't use either form (or anything like this) in production code. Overusing globals makes it way, way too easy to lose track of what information is coming from where, what is getting modified where, and makes it tougher to reuse and maintain the code. -- +----------------------------------------------------------------+
|Torben Wilson <torben@php.net>                     Netmill iTech|
|http://www.coastnet.com/~torben            http://www.netmill.fi|
|Ph: 1 250 383-9735                             torben@netmill.fi|
+----------------------------------------------------------------+


« previous php.general (#19272) next »