MyBB Community Forums

Full Version: "global" in PHP is the devil?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello,

I'm reading everywhere (especially on 2 PHP OOP books) that using "global" variables inside methods (functions) is the devil, and should never be used, instead they should be passed as arguments to avoid "dependency", which means :

If all my classes depend on a $date variable, if I unintentionally change the $date variable the whole script is going to be a mess...

The person who was defending that said :
A method is designed to get a list of arguments, do some action, and then return (or not) a value. But when a third party variables exist in that method, that defeat the whole purpose of arguments, if we go that way, there's no need to pass arguments at all, since you can always set the variables you need as global, do the same action and return the value, it's still procedural, it's still spaghetti code...


Though, phpBB / MyBB / SMF are using "global" in many classes and methods...

Quote:In computer programming, a global variable is a variable that is accessible in every scope (unless shadowed). Interaction mechanisms with global variables are called global environment (see also global state) mechanisms. The global environment paradigm is contrasted with the local environment paradigm, where all variables are local with no shared memory (and therefore all interactions can be reconducted to message passing).

They are usually considered bad practice precisely because of their non-locality: a global variable can potentially be modified from anywhere (unless they reside in protected memory or are otherwise rendered read-only), and any part of the program may depend on it.[1] A global variable therefore has an unlimited potential for creating mutual dependencies, and adding mutual dependencies increases complexity. See action at a distance. However, in a few cases, global variables can be suitable for use. For example, they can be used to avoid having to pass frequently-used variables continuously throughout several functions. Global variables also make it difficult to integrate modules because software written by others may use the same global names unless names are reserved by agreement, or by naming convention.

http://en.wikipedia.org/wiki/Global_variable



I'm developing a script and I can say that it's going to be very hard for me to re-code everything into a non-global environment :s

I'd really like to read your thoughts on this...
Global is not the devil, if used properly.

Look at what MyBB does globalize:
$db, $mybb, and $config are the big ones. Also, $db and $mybb both point to classes that allow us to handle basically everything we need to do. Without recreating them every time.
Hey Dylan, thanks for replying, well to be honest I got my inspiration from MyBB code, and it's totally normal (I think) to use global on those variables that are very necessary, it's rather pointless to pass the $db object as an argument to every other class...

Anyway, I'm still going to code with "global" unless PHP Community "deprecates" it..

Now it's true that bad programmers can really do spaghetti code if they use "global" everywhere... But if used properly (as you stated), it's not harmful Smile
It can be bad if used bad, but otherwise its more of a performance issue, though slight in most cases. However in a system like MyBB where plugins are used, its not always possible to use local variables.

I prefer to minimize global variables, but since PHP is not compiled code, global in PHP is actually local in scope since the entire program (i.e. MyBB) is not loaded on every call, only the pieces it needs are loaded. This is unlike compiled code (.NET) where the entire application is loaded and all global variables are residing in memory all the time.
Globals are good for things you only need one of, are used alot, and should be the same throughout the whole system. Like mentioned, MyBB's $db and $mybb globals are good examples. Arguments are for things that have limited usefulness (only within a certain place), or for things that change a lot within a certain scope (i.e. a loop index).

There are little things about MyBB's coding practices I don't like, but other than those nitpicks most little bits of MyBB's source are a good place to see reasonable software architecture. Maybe not the best, but most certainly better than a lot of stuff out there.
I view it simply as no choice. It's how MyBB is coded. I'm sure that in 2.0 that it will be all OOP instead. For now code your plugins as global variables to adhere to current standards. Future standards we will all deal with when it's time.
(2012-01-12, 12:04 AM)labrocca Wrote: [ -> ]I view it simply as no choice. It's how MyBB is coded. I'm sure that in 2.0 that it will be all OOP instead. For now code your plugins as global variables to adhere to current standards. Future standards we will all deal with when it's time.

Except he's talking about a non MyBB project here Wink
(2012-01-12, 12:36 AM)Dylan M. Wrote: [ -> ]
(2012-01-12, 12:04 AM)labrocca Wrote: [ -> ]I view it simply as no choice. It's how MyBB is coded. I'm sure that in 2.0 that it will be all OOP instead. For now code your plugins as global variables to adhere to current standards. Future standards we will all deal with when it's time.

Except he's talking about a non MyBB project here Wink

True that :p
If you use an OOP framework then yes you probably shouldn't use it. We use them in some classes as yes, they're classes, but it's not in an OOP structure. If you're using procedural PHP with standard functions like we do you can't pass everything as a paramater so you globalise; in OOP you'd extend classes etc and have things available with $this
If you're starting something from scratch you shouldn't require globals in your methods unless you're not doing it right. Otherwise if you just want to use OOP together with a procedural script like MyBB you've got no choice unless you want to have a huge list of arguments.
Pages: 1 2