OM2 Posted July 27, 2008 Share Posted July 27, 2008 I've read that using global is a big no no. And instead, you should use variables and pass them into functions. I have declared an array, $myVariables, in which I store all of my variables that I want functions to access. I wanted to know, is it 'OK' if I use: global $myVariables; Just wanted to know if it's good practice or not. I find that having to pass in the extra arguement of the global variable array is annoying and slightly illogical because it doesn't really make sense to the function functionality. (Not sure if that makes sense!) Thanks. OM Link to comment https://forums.phpfreaks.com/topic/116857-solved-question-about-using-global/ Share on other sites More sharing options...
Nhoj Posted July 27, 2008 Share Posted July 27, 2008 Personally I think using globals is very easy and convenient, for example you could do: $array = array('stuff1', 'stuff2'); And function myFunction() { global $array; } With it it just means that for the sake of that function it will pull the $array variable from outside the script. Very helpful and convienent. For example, if you used the previously said statement 100 times throughout your mega web script and then one day you need to add one more array to be globally included for the function. You'd only have to add one more line of code inside the function. If you were to do myFunction($array); every single time you wanted to call the function so that it always included the $array, it could get quite cluttered and complicated later on if you are already including 5 or 6 variables when using the function and 1 week later you need to add that array. Would you rather modify every single call to myFunction or simply add one line inside the function itself. Link to comment https://forums.phpfreaks.com/topic/116857-solved-question-about-using-global/#findComment-600898 Share on other sites More sharing options...
OM2 Posted July 27, 2008 Author Share Posted July 27, 2008 thanks for the quick reply. i've actually passed it in as an arguement. i think i'll leave as is: to man more code edits required otherwise. i'll do as u suggest for future. thanks. Link to comment https://forums.phpfreaks.com/topic/116857-solved-question-about-using-global/#findComment-600900 Share on other sites More sharing options...
trq Posted July 27, 2008 Share Posted July 27, 2008 i'll do as u suggest for future. Don't. The method you have used (passing the array into the function) should always be the preffered method. Using the global keyword makes it harder to decifer where exactly this magical variable is defined, it also makes it easier to be overidden accidently. Link to comment https://forums.phpfreaks.com/topic/116857-solved-question-about-using-global/#findComment-600904 Share on other sites More sharing options...
OM2 Posted July 27, 2008 Author Share Posted July 27, 2008 ok... that makes sense. but, my think was just that: ur only doing it once... so it won't do any harm? Link to comment https://forums.phpfreaks.com/topic/116857-solved-question-about-using-global/#findComment-600907 Share on other sites More sharing options...
PFMaBiSmAd Posted July 27, 2008 Share Posted July 27, 2008 I'll echo what thorpe has already stated. Don't use the global keyword to bring variables into a function. If your function is that closely tied to specific variables, you should be using an OOP class to wrap both the variables and the function together or the code you are trying to make into a function is main program logic and is inappropriate to be in a function at all. Link to comment https://forums.phpfreaks.com/topic/116857-solved-question-about-using-global/#findComment-600932 Share on other sites More sharing options...
OM2 Posted July 27, 2008 Author Share Posted July 27, 2008 thanks for the reply. i haven't delved in oo as yet: but will do very soon. Link to comment https://forums.phpfreaks.com/topic/116857-solved-question-about-using-global/#findComment-600940 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.