1

so how do you prefer to declare and use global variables? 1)

global variable;
echo $variable;

or 2)

echo $GLOBALS['variable'];

?

Which is the less bad method? :)

edit:

or 3)

class myglobalstuff{

  static $instance;

  public static function foo(){
    if(!(self::$instance instanceof self))
      self::$instance = new self();

    return self::$instance;
  }
}
...

?

Alex
  • 1,375
  • 2
  • 9
  • 10
  • For inside class methods, accessing properties of the class? If that's the case, I'd say `$this->variable` would be much better. – Ry- May 06 '11 at 16:22

7 Answers7

4

Using $GLOBALS is less bad. But if you can, avoid globals :)

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
2

The PHP manual explains how to define and use global variables: $GLOBAL

Yuck
  • 49,664
  • 13
  • 105
  • 135
2

The global keyword doesn't create a global variable as such, it simply tells the script to treat the variable with global scope.

The $GLOBALS array is actually a superglobal variable, which is slightly different.

More info here, it's a good read: http://php.net/manual/en/language.variables.scope.php

Nick Brunt
  • 9,533
  • 10
  • 54
  • 83
  • Like Nick said, it is pointless to do the following in *globally scoped code* (code not inside a function or method declaration): global $foo; $foo = "someValueForEverything"; $foo is global because it is declared globally (not because of the "global" keyword). One would use "global" *inside* a function or method that needs to reference the global $foo (else a local $foo would come into existence). – grantwparks Aug 07 '11 at 19:42
1

We prefer to not use global variables at all. And same goes for Singletons and Registries and every other way of having a global state.

You would be much better off if you learned how to write a proper OOP code.


update

Few links and related information:

tereško
  • 58,060
  • 25
  • 98
  • 150
  • 1
    @Alex : no there are only cases, when you application design is flawed. [The Clean Code Talks - "Global State and Singletons"](http://www.youtube.com/watch?v=-FRm3VPhseI) – tereško May 06 '11 at 16:26
  • I would really be interested if you could expand on the proper methods or link to some reading material about the proper methods. – Jim May 06 '11 at 16:26
  • @Brad what sort of materials are you actually after ? If you want to know what you should avoid global state, then in code, then web is full of links. If you want the books , then scroll down to the bottom of [this answer](http://stackoverflow.com/questions/5895805/properly-calling-the-database-from-model-in-an-mvc-application/5906211#5906211) , there you will find the books i would recommend ( there is not enough space to put them in comment ) – tereško May 06 '11 at 16:31
  • @teresko: that video is about java. I don't know java :( maybe it's better than php? Anyway, the only way to avoid globals in PHP is to pass parameters like crazy between functions. Doesn't this create even more confusion than using a global variable? – Alex May 06 '11 at 16:38
  • @teresko, you can edit your answer and include that link :) It is good material and I plan on doing some reading on it. Thanks for the link. – Jim May 06 '11 at 16:42
  • @teresko just because globals and singletons aren't your style doesn't mean they're not equally appropriate and "proper" -- it depends on what you're after. abandoning OOP entirely is the fastest way to run PHP, but I still prefer OOP. – Kelly May 06 '11 at 16:42
  • @Alex: Parameter passing is better. Globals generate weird dependencies. Maybe OOP is a solution. – Felix Kling May 06 '11 at 16:44
  • ok, but what if you end up passing for example a $db object to a function that doesn't use it, but it needs to pass it further to a function that uses it? looks weird... – Alex May 06 '11 at 16:47
  • @Alex watch that video. It has nothing to do with java and everything to do with application design. – tereško May 06 '11 at 16:49
  • very interesting video, but I didn't quite understand the answer he gave to the question around ~32:20. You may need the the db layer only at the start of your app and at the end, so how can you use it without instantiating it twice and not passing it as parameters to all layers from the start to the end? – Alex May 07 '11 at 00:15
1

Putting aside for a minute how evil global variables may be, let me break this down to a more generic problem that I've spotted: using string array keys instead of variables.

Typing string array keys may lead to typos which may be tricky to spot: $GLOBALS['var1'] vs $GLOBALS['varl'] (one vs lamda).

If you have a modern IDE that does autocompletion you will find it useful to declare global $variable and then, when typing it in, to invoke autocompletion to get an indication you didn't make a typo.

Such a modern IDE may also have occurences highlighting which will help with avoiding typos (you can see if it appears nearby) as well as navigating your code. It, again, will only work with variables and not array keys.

If you need to use globals then using $GLOBALS may be better in reminding you where the variable came from when you're hunting down some value. It might, however, pay off to atleast define the variable names as a constant to take advantage of IDE autocomplete and occurence highlighting: $GLOBALS[_VAR1].

Ofcourse these nifty features also work with class variables which is yet another reason to consider refactoring :)

Fanis Hatzidakis
  • 5,282
  • 1
  • 33
  • 36
0

I'd go with $_SESSION['var'] over $GLOBALS. It does more or less the same, but safer. Edit: I meant to say more preferred method. But $GLOBALS was said to be depreciated soon anyway.

robx
  • 3,093
  • 2
  • 26
  • 29
  • It's not and stuffing things into the session just to make them global is a bad idea because it may have adverse affects on the application (since it persists the data). If you REALLY need globals, then I would stick it behind a singleton, but my suggestion would be to pass the data as parameters. – John Cartwright May 06 '11 at 16:27
  • @John, you can clear the session after your done with it by running session_destroy() and unset(), so it's not that bad if you use it properly to bring it back and forth from page to page as global. – robx May 06 '11 at 16:30
  • Agree with John, this is kind of a technically ridiculous suggestion, no offense intended personally. – grantwparks Aug 07 '11 at 19:35
  • To each their own I guess. No offense taken ;) – robx Aug 07 '11 at 22:12
0

Also, ignoring the "no globals" argument, I'd like to make a fine distinction about the use of the "global" declaration, because I've seen it misused all over. At least in PHP 5, it is pointless to do the following in globally scoped code (code not inside a function or method declaration):

global $foo;
$foo = "someValueForEverything";

$foo is global because it is declared globally (not because of the "global" keyword). One would use "global" inside a function or method that needs to reference the global $foo (else a local $foo would come into existence).

grantwparks
  • 1,124
  • 9
  • 13