OldWolf Posted February 29, 2008 Share Posted February 29, 2008 This is just a quick two part question that's been bugging me recently: It seems to me the few 'major' php projects I've had a look at (like phpbb) never use $_GLOBAL, nor do they globalize their classes. Is this just considered sloppy codework, and if so why (this may be obvious, but I look at things the wrong way some times, lol)? Thanks, OldWolf Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 1, 2008 Share Posted March 1, 2008 $_GLOBAL is rarely used in any code, because properly written code does not need to use it. "globalize their classes" could mean anything. Can you give an example. Perhaps if you tell us why you believe not using these is bad, we can provide an answer as to why they are or are not. The purpose of a function is to receive some input, perform a function on it, and return the results. Functions compartmentalize code and data and allow you to re-use the same code over and over. By using something like the $_GLOBAL array or the global keyword, you are tying a specific piece of data in the main program to that function, which eliminates the general purpose nature of the function. For comparison purposes, do you know of any built-in php function that requires you to setup a specifically named variable in the main program before you call the function? The answer is no, because for functions to be re-usable and compartmentalized, you don't write them to directly access a main program variable. Quote Link to comment Share on other sites More sharing options...
trq Posted March 1, 2008 Share Posted March 1, 2008 Beutifully put PFMaBiSmAd. I'm going to be quoting this in the future as this and similar questions get asked all the time. Quote Link to comment Share on other sites More sharing options...
OldWolf Posted March 1, 2008 Author Share Posted March 1, 2008 $_GLOBAL is rarely used in any code, because properly written code does not need to use it. "globalize their classes" could mean anything. Can you give an example. Perhaps if you tell us why you believe not using these is bad, we can provide an answer as to why they are or are not. The purpose of a function is to receive some input, perform a function on it, and return the results. Functions compartmentalize code and data and allow you to re-use the same code over and over. By using something like the $_GLOBAL array or the global keyword, you are tying a specific piece of data in the main program to that function, which eliminates the general purpose nature of the function. For comparison purposes, do you know of any built-in php function that requires you to setup a specifically named variable in the main program before you call the function? The answer is no, because for functions to be re-usable and compartmentalized, you don't write them to directly access a main program variable. Excellent, thank you, that answers my question. When I said globalize their classes, what I should have said was objects... don't know where my head was at there. I think it also would have helped for me to mention a good example. In phpbb, they frequently use the variable $phpbb_root_path (I'm pretty sure that's the exact name, doing it off memory). At any rate, they have to global it into a lot of their functions when they want to use it. The same goes for the $db object. That's where my question was rooting from... I was simply wondering why they didn't global the two so that they were accessable without globalizing in each function they wanted them in. Quote Link to comment Share on other sites More sharing options...
trq Posted March 1, 2008 Share Posted March 1, 2008 If phpBB was designed properly they would pass these variables into there functions as arguments. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 1, 2008 Share Posted March 1, 2008 Or better yet, OOP would be used and during the initialization step, when the class constructor is executed, any application configuration file or database would be read and used to set any configuration settings (which should in most cases be defined constants) that are used in the rest of the application. Quote Link to comment Share on other sites More sharing options...
OldWolf Posted March 1, 2008 Author Share Posted March 1, 2008 If phpBB was designed properly they would pass these variables into there functions as arguments. Some of those functions require a lot of global objects or variables... is there a way to go about it without globals or arguements? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.