Forum Moderators: coopster

Message Too Old, No Replies

Alterative to global variables in php functions?

         

benj0323

1:43 am on Nov 17, 2004 (gmt 0)

10+ Year Member



I wrote a function that needs to use every variable it can, as if it was coded into the php file and not called by a function.

Is there some way to not have to globalize every variable I want to use? Because I may be adding, deleting, or changing variables.

For example:

function some_func()
{
global $any_variable_i_want_to_use_in_here;

a bunch of code
}

instead of have to do global $any_variable_i_want_to_use_in_here; is there a way I can make the function public or use any variable available?

Thanks for your help.

Slade

2:03 am on Nov 17, 2004 (gmt 0)

10+ Year Member



Without taking any effort to weigh good vs bad, you could wrap all your varables into one or more arrays, and just global them.

If you want to explode the globals inside your function, you could keep most of your code the same after modifying the global line and adding the explode code.

I am interested in hearing other people's thoughts on this and other ideas.

Salsa

2:48 am on Nov 17, 2004 (gmt 0)

10+ Year Member



Actually, for security reasons, you should turn global variables entirely off in you php.ini file, then access variables within functions as $name = $_POST[name] or $name = $_GET[name], etc. One or other ways, you can access the variables you need in this way, with global variables off. For a test, try including

?><pre><? echo "\$GLOBALS ="; print_r($GLOBALS);?></pre><?

in your script to see what variables are avilable to you in this way.

Slade

3:21 am on Nov 17, 2004 (gmt 0)

10+ Year Member



I think the original poster was referring to variables they were defining in other parts of their script, things internal to the specific application.

I know I wasn't talking about that kind of global. Yes, the php config option register_globals should be set to off for security reasons.

Salsa

3:47 am on Nov 17, 2004 (gmt 0)

10+ Year Member



Slade,

If that's what he meant, I agree, except that I'd just wrap them all in an array and use it as an argument of the function.

I wish you well,
Salsa

benj0323

6:59 am on Nov 17, 2004 (gmt 0)

10+ Year Member



I was talking about globals in a php function, not registerred globals. I turned registerred globals off as soon as possible because I know its a security risk. So yes, I do use $_GET and $_POST.

Let me try to clarify what I was asking.

Lets say I call a function in my php code:

some_func();

Lets say before some function I have a variable:

$some_variable

If I want to use that variable in my function I have to do this:

function some_func()
{
global $some_variable;

function code
}

The problem is that in the function I use the eval() function. So it evaluates anything i pass into the eval() function and whatever I pass into the eval() function could contain basically any variable in my code. The variables are going to be different in every instance and will probably contain quite a few variables. So instead of having to global hundreds of variables I was wondering if there was a way to make the function accept any variables without having to global variables.

I hope this clarifies what I was asking.

mincklerstraat

7:50 am on Nov 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're already using
eval()
, so I won't go into how aweful - ugly the following type of solution is:


$this = 1;
$that = 2;
function thisthat(){
extract($GLOBALS);
echo $this.' '.$that;
}
thisthat();

it comes down to extracting

$GLOBALS
inside your function.

benj0323

8:16 am on Nov 17, 2004 (gmt 0)

10+ Year Member



Worked perfect. Thanks for your help. What do you mean by awful and ugly?

Both eval and extract($GLOBALS) is time consuming?