2

Is it a problem if you use the global keyword on variables you don't end up using? Compare:

function foo() {
    global $fu;
    global $bah;
    if (something()) {
        $fu->doSomething();
    } else {
        $bah->doSomething();
    }
}

function bar() {
    if (something()) {
        global $fu;
        $fu->doSomething();
    } else {
        global $bah;
        $bah->doSomething();
    }
}

I'm quite aware that using the second method makes maintaining this code much harder, and that it's generally preferred to put all your globals at the start of functions, so: Ignoring the difference in maintainability and code-styling of the two functions, is there a difference between these two in terms of overhead?

nickf
  • 537,072
  • 198
  • 649
  • 721

4 Answers4

8

If there is, it won't be (humanly) measurable, unless you are literally calling this function millions of times. And even if it was a recursive function with that property, I still wouldn't use your second method for the maintainability aspects you already brought up.

Edit: For arguments sake, I actually went and benchmarked this, and bar() ended up slower by 0.1s over one million calls. Which means performance wise, you still have a reason to use the cleaner version.

Matthew Scharley
  • 127,823
  • 52
  • 194
  • 222
1

As monoxide said, there's no significant performance difference.

However, I'd avoid using global if at all possible; it's a bad road to go down and you'll end up with spaghetti. Use a static class; it'll keep things much better organized.

dirtside
  • 8,144
  • 10
  • 42
  • 54
  • While I agree with the first part, I don't see much benefit of using a static class. Globals should be avoided in all its forms. – troelskn Oct 08 '08 at 10:49
  • I use global to make Main object available inside function and at the start of scripts just like i would call a require or include. It make it much more cleaner for some functions like http://php.net/manual/en/function.imagecopyresized.php that already have 10 parameter by itself. In some case, global makes it WAY cleaner and can save lots of code. Also, calling global makes them available by reference, you can alter the variable value inside your function without a return (instead of the &marker). Global was meant to be used... in some cases regards – Louis Loudog Trottier Mar 07 '17 at 03:43
0

In case you don't know, you can do the following:

function foo() {
    global $fu, $bah;
    if (something()) {
        $fu->doSomething();
    } else {
        $bah->doSomething();
    }
}

You can put both of the globals in the same line. Might even make it faster :)

Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
-1

Global variables are generally considered very bad style. I would claim that whenever you need to use the global keyword, or a static class property (And thus, including the infamous Singleton), you should seriously reconsider what you're doing. It may be slightly more work to avoid globals, but it's a huge bonus to code maintainability. This particular example, might be better expressed with:

function foo($fu, $bah) {
  if (something()) {
    $fu->doSomething();
  } else {
    $bah->doSomething();
  }
}

If you don't like passing a lot of parameters around, you may use classes to encapsulate them, or perhaps it is a sign that you should factor your code differently.

troelskn
  • 115,121
  • 27
  • 131
  • 155
  • i agree: you don't want to go willy-nilly making things global variables - it's messy, doesn't scale well and doesn't play well with others (ie: when you combine your code with others'). BUT there are still good uses for it, and you can't just ignore it. Passing parameters each time does not work. – nickf Oct 08 '08 at 12:37