views:

658

answers:

3
function foo () {
    global $var;
    // rest of code
}

In my small PHP projects I usually go the procedural way. I generally have a variable that contains the system configuration, and when I nead to access this variable in a function, I do global $var;.

Is this bad practice?

+14  A: 

When people talk about global variables in other languages it means something different to what it does in PHP. That's because variables aren't really global in PHP. The scope of a typical PHP program is one HTTP request. Session variables actually have a wider scope the PHP "global" variables because they typically encompass many HTTP requests.

Often (always?) you can call member functions in methods like preg_replace_callback() like this:

preg_replace_callback('!pattern!', array($obj, 'method'), $str);

See callbacks for more.

The point is that objects have been bolted onto PHP and in some ways lead to some awkwardness.

Don't concern yourself overly with applying standards or constructs from different languages to PHP. Another common pitfall is trying to turn PHP into a pure OOP language by sticking object models on top of everything.

Like anything else, use "global" variables, procedural code, a particular framework and OOP because it makes sense, solves a problem, reduces the amount of code you need to write or makes it more maintainable and easier to understand, not because you think you should.

cletus
It should be noted that PHP 5.3 does address some of this with lambda functions allowing you to avoid using function declared in global scope for callbacks. +1 for maintainable, readable code advice
Jonathan Fingland
Can you not use a callback of the form `array ($obj, 'callbackMethod')` in calls to `preg_replace_callback()`? (I know, I've fallen prey to this OOP pitfall...)
grossvogel
@grossvogel yes, updated.
cletus
+3  A: 

i agree with cletus. i would add two things:

  1. use a prefix so you can immediately identify it as global (e.g. $g_)
  2. declare them in one spot, don't go sprinkling them all around the code.

best regards, don

Don Dickinson
Jeah, I always prefix variables that I intend to use globally with a underscore.
KRTac
+1  A: 

Global variables if not used carefully can make problems harder to find. Let's say you request a php script and you get a warning saying you're trying to access an index of an array that does not exist in some function.

If the array you're trying to access is local to the function, you check the function to see if you have made a mistake there. It might be a problem with an input to the function so you check the places where the function is called.

But if that array is global, you need to check all the places where you use that global variable, and not only that, you have to figure out in what order those references to the global variable are accessed.

If you have a global variable in a piece of code it makes it difficult to isolate the functionality of that code. Why would you want to isolate functionality? So you can test it and reuse it elsewhere. If you have some code you don't need to test and won't need to reuse then using global variables is fine.

rojoca