kellan4459 Posted January 31, 2012 Share Posted January 31, 2012 I am using a MVC framework and in my controller I have defined a class variable for configurations. In my action I have a call to the configuration class to set the class variable to the current configurations. public $configArray = array(); public function actionBuild($id) { $this->configArray=Config::model()->getConfigArray($id); $this->buildStep1(); ... } When I echo the configuration in the method it is 10 but when I echo in buildStep1 it is 11. What is the proper way for configArray to be global and updated when I call getConfigArray for use in functions in the class? Quote Link to comment https://forums.phpfreaks.com/topic/256087-proper-use-of-globals-and-variable-scope/ Share on other sites More sharing options...
scootstah Posted January 31, 2012 Share Posted January 31, 2012 Not really sure what you are asking here. Can you be more specific? Quote Link to comment https://forums.phpfreaks.com/topic/256087-proper-use-of-globals-and-variable-scope/#findComment-1312849 Share on other sites More sharing options...
kellan4459 Posted January 31, 2012 Author Share Posted January 31, 2012 I changed from $this->variable_name to global declaration of the variable. That fixed my issue. Quote Link to comment https://forums.phpfreaks.com/topic/256087-proper-use-of-globals-and-variable-scope/#findComment-1313035 Share on other sites More sharing options...
KevinM1 Posted January 31, 2012 Share Posted January 31, 2012 Globals and OOP are fundamentally opposite ideas. Globals are generally bad regardless, as they lead to spaghetti code. EDIT: See http://www.phpfreaks.com/forums/index.php?topic=351194.msg1659987#msg1659987 Quote Link to comment https://forums.phpfreaks.com/topic/256087-proper-use-of-globals-and-variable-scope/#findComment-1313036 Share on other sites More sharing options...
scootstah Posted January 31, 2012 Share Posted January 31, 2012 I changed from $this->variable_name to global declaration of the variable. That fixed my issue. I still have no idea what you are talking about. Quote Link to comment https://forums.phpfreaks.com/topic/256087-proper-use-of-globals-and-variable-scope/#findComment-1313051 Share on other sites More sharing options...
AyKay47 Posted January 31, 2012 Share Posted January 31, 2012 I changed from $this->variable_name to global declaration of the variable. That fixed my issue. as stated, this is a bad practice as it can lead to pollution down the road. Any value that your method needs should be brought in via its parameter list, not globally. Quote Link to comment https://forums.phpfreaks.com/topic/256087-proper-use-of-globals-and-variable-scope/#findComment-1313053 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.