views:

97

answers:

6

Hello!

Is there a best practice / recommandation when I want to use a variable declared outside of a function when it comes to using:

  1. global $myVar
  2. $GLOBALS['myVar']

Thank you.

+3  A: 

I would go for global in the top of your function. In that way, you can easily see what globals are used.

Lekensteyn
Except when the function is long (e.g. a big switch statement), and now you don't know what's a global and what's not. You can declare which globals you use with a phpdoc (`@global`).
Artefacto
+12  A: 

What you should really do is pass the variable to the function instead of using a global at all.

An example how to change a variable outside of the function via passing it as reference parameter:

function myFunc(&$myVar)
{
    $myVar = 10;
}

$foo = 0;
myFunc($foo);
var_dump($foo); // yields 10
reko_t
I need to edit an external variable.
Francisc
@Francisc If you really want to, you can pass it by reference.
Artefacto
I attached an example to demonstrate using a reference like Artefacto suggested.
reko_t
What I want to do is have an $output variable that gets concatenated with new data contantly and instead of $output.='something' I want to do writeFunction('something').The function itself would be function writeFunction($str){global $output;$output.=$str;)Is there a better way of doing that?
Francisc
`function writeFunction( }` and to use it: `writeFunction($output, "something");`
reko_t
Ok, so you would say that is much better than using globals $output, correct?
Francisc
Yes. Functions written in this way are not limited to only dealing with global variables. You can also use them on variables declared on local scopes. Nor are they limited to using a variable from outside with specific name.
reko_t
Thank you very much.
Francisc
A: 

Ofcause, 'global' is right way. Global variables setting in PHP using prefix global.

Alexander.Plutov
I wouldn't say it's 'of course', at all. I wouldn't even agree. And what do you mean with "Global variables setting in PHP using prefix global."?
pinkgothic
+1  A: 

I wold say, it's recommended not to use Globals in OOP, there should always be a better way

Tokk
+4  A: 

Well, you should only use globals in limited circumstances, but to answer your question:

  1. global is potentially marginally faster (it will rarely make a difference).
  2. $GLOBALS (not $GLOBAL) is more readable, because every time you see it, you know you are accessing/changing a global variable. This can crucial in avoiding nasty bugs.
  3. Inside a function, if you want to unset a global variable, you must use unset($GLOBALS['varname']), not global $varname; unset($varname);.

As to points 1 and 2, I'll quote Sara Golemon here:

What does that mean for your use of the $GLOBALS array? That's right, the global keyword is technically faster. Now, I want to be really clear about one thing here. The minor speed affordance given by using your globals as localized [compiled variables] needs to be seriously weighed against the maintainability of looking at your code in five years and knowing that $foo came from the global scope. something_using($GLOBALS['foo']); will ALWAYS be clearer to you down the line than global $foo; /* buncha code */ something_using($foo); Don't be penny-wise and pound foolish..

Artefacto
Very helpful, thank you.
Francisc
A: 

global $var; is equivalent to $var =& $GLOBALS['var'].

Some people suggested that it is faster than using $GLOBALS, however it's not necessarily the case. If you use the variable only once, $GLOBALS will be faster, because you won't waste time for assignment.

However, if you do use the variable multiple times, using global (or the equivalent assignment) is faster, because search the array for the var key only once.

That's it about speed. However, the speed difference is really small, and readability is more important. However, different people have different preferences about readability -- I prefer global, some other people answering here prefer $GLOBALS, so it's up to you to decide what looks better.

Mewp
Artefacto
@Artefacto: Hm, I thought that they were really equivalent. Thanks for pointing that out, I'll try to find more information about this.
Mewp
As to well, the second is wrong, run "Profile code" on this http://codepad.viper-7.com/roRI8g You'll see that `global` is just one hash lookup plus storage in a compiled variable. The `$GLOBALS` variant entails two hash lookups. It's not stored in any variable, but it doesn't make any difference versus storing in a compiled variable, performance wise.
Artefacto
@Artefacto: I see now. Also, http://codepad.viper-7.com/YfjsII shows that the assignment indeed uses one more opcode than global.
Mewp