[nycphp-talk] Question about variable variables
John Lacey
jlacey at att.net
Fri Feb 13 16:36:00 EST 2004
Chris Shiflett wrote:
> --- Daniel Porcher <danporcher at earthlink.net> wrote:
>
>>Is there a consensus about the use of variable variables?
>
>
> I don't think so. I know a lot of people who think it is bad practice.
> Their reasoning is never technical; it's social. They argue that variable
> variables are more likely to confuse another programmer who looks at your
> code, sort of like when you cram too many operations on a single line.
>
> In my opinion, there are situations where it just happens to be the most
> elegant solution. But, I certainly feel outnumbered on this one. :-)
Daniel, here's a function from phpTest to illustrate:
(excuse the email formatting)
/**
* Registers global variables
*
* This function takes global namespace $HTTP_*_VARS
variables from input and if they exist,
* register them as a global variable so that scripts can
use them. The first argument
* signifies where to pull the variable names from, and
should be one of GET, POST, COOKIE, ENV, or SERVER.
*
*/
function pt_register()
{
$num_args = func_num_args();
$vars = array();
if ($num_args >= 2) {
$method = strtoupper(func_get_arg(0));
if (($method != 'SESSION') && ($method != 'GET')
&& ($method != 'POST') && ($method != 'SERVER') && ($method
!= 'COOKIE') && ($method != 'ENV')) {
die('The first argument of pt_register must be
one of the following: GET, POST, SESSION, SERVER, COOKIE,
or ENV');
}
$varname = "HTTP_{$method}_VARS";
global ${$varname};
for ($i = 1; $i < $num_args; $i++) {
$parameter = func_get_arg($i);
if (isset(${$varname}[$parameter])) {
global $$parameter;
$$parameter = ${$varname}[$parameter];
}
}
} else {
pt_exit('You must specify at least two arguments');
}
}
More information about the talk
mailing list