2012-01-11, 10:01 PM
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...
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...
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...