Are Global Variables in PHP Considered Bad Practice? If So, Why

Are global variables in PHP considered bad practice? If so, why?

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 than 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.

PHP - why global variables are evil

I answer this question:

will the echoed $variable1 be displayed as what the respective users have input to their browser or it will override the first user's input by the last user's input?

The echoed $variable1 will be displayed as what the respective users have input to their browser.

PHP makes thread per request, so different requests have different variables (include global).

I check this post.

As said by JasonB,
PHP global variable has a very wide scope, and make code complex.

Why is it considered bad practice to use global reference inside functions?

I think a top reason for avoiding this is that it hides dependencies.

Your functions get_data and run_html do not advertise in any way that they share data, and yet they do, in a big way. And there is no way (short of reading the code) to know that run_html will be useless if get_data has not been called.

As the complexity of your codebase grows, this kind of lurking dependency will make your code fragile and hard to reason about.

Is it bad practice to use a global $config variable?

IMHO, as long as a global variable is not simulatenously modified by different processes (i.e. it's read-only for all of them), it is OK to have it in the global scope.

A way to simulate a read-only variable is to encapsulate it in a global object with a private variable in it and a public method that would return that variable.

Example:

class Config {
private $conf = array(/*read from file?*/...);

function getconf() {
//AFAIK, any attempt to write into the returned array will make PHP's
//core to duplicate it, so the original config won't be modified
//(as long as $conf does not contain objects, which are always addressed
//by reference)
return $conf;
}
}

Why are global variables considered bad practice?

They clutter up the global namespace and are slower to look up than local variables.

First of all, having many global variables is always a bad thing because it's easy to forget you declared a variable somewhere and accidentally re-declare it somewhere else. If your first variable was local then you don't have a problem. If it was global, then it just got overwritten. This gets even worse when you get into implied globals (e.g. when you say someVar = someValue without declaring someVar with the var keyword).

Secondly, global variables take longer for Javascript to "find" than local variables. The difference in speed isn't huge, but it does exist.

For further reading and a more in-depth explanation of why globals are considered bad practice, you may want to check out this page.



Related Topics



Leave a reply



Submit