Re: Good way to make variables global (?)

From: Date: Tue, 10 Oct 2000 01:24:20 +0000
Subject: Re: Good way to make variables global (?)
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-19280@lists.php.net to get a copy of this message
Erica Douglass writes: > 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. [trimmed] Well, I'm not going to flame you for it. God knows my code isn't perfect. :) But one possibility for getting past that is to have form arrays for this sort of thing which keep all of the variables together without the need for extra bookkeeping to track them. They'd then show up as subarrays of $HTTP_POST_VARS, making each group easy to pick out and use only where needed. Something like: ---mailtmpl.inc--- Recipient name: <input type="text" name="mail[name]" value="<?php echo $mail['name']; ?>"> Recipient address: <input type="text" name="mail[addr]"> value="<?php echo $mail['addr']; ?>"> Message: <textarea name="mail[msg]"><?php echo $mail['msg']; ?> </textarea> <input type="submit" name="formhandler[action]" value="Submit"> ---mailtmpl.inc--- ...you'd get $mail defined in the global namespace if you have register_globals on, or $HTTP_POST_VARS['mail'] in any case (at least, with recent versions of PHP 4). This even makes it easier to loop through the values for validation, and allows you to even set default values for unchecked checkbox values, which don't get set to any value in an HTTP submission. And you just pass in the subarray (or even a reference to it), and get the same result without having to loop through anything (as even extract() loops, just internally). Just a thought... -- +----------------------------------------------------------------+ |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 (#19280) next »