diocyria Posted September 18, 2010 Share Posted September 18, 2010 Hello All, I am fairly new to PHP class development, and I was wondering if it is normal behaviour to see classes not being able to access global variables related to PHP-based requests ($_GET, $_SESSION, etc.)? I seem to either have to use "global <var>" inside of the class to access the data, or I am forced to change the methods so then such required data is passed in as parameters. Perhaps I am just doing something wrong? Here is an example of what I mean: class Something { function aFunc() { if (isset($_SESSION['somedata'])) { return false; } // this always returns false whether I had set the value or not } function bFunc() { global $_SESSION; // figured this would already be in a global accessible scope if (isset($_SESSION['somedata'])) { return false; } // now this will return false only when the value is set } } Now the above example is commented to demonstrate what I was meaning above, and I was wondering if this was the 'norm' when developing classes for PHP (and thus such values should be passed as parameters)? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/213726-classes/ Share on other sites More sharing options...
diocyria Posted September 18, 2010 Author Share Posted September 18, 2010 ooops the above example should be (sorry I typed it up really quick the first time): class Something { function aFunc() { // this always returns true whether I had set the value or not if (isset($_SESSION['somedata'])) { return false; } return true; } function bFunc() { global $_SESSION; // figured this would already be in a global accessible scope if (isset($_SESSION['somedata'])) { return false; } // now this will return false only when the value is set return true; } } There, hopefully that makes a little more sense. Quote Link to comment https://forums.phpfreaks.com/topic/213726-classes/#findComment-1112453 Share on other sites More sharing options...
KevinM1 Posted September 18, 2010 Share Posted September 18, 2010 The superglobal arrays are just that - always global, and thus always available. There has to be something else at play. Show us your real code. Quote Link to comment https://forums.phpfreaks.com/topic/213726-classes/#findComment-1112485 Share on other sites More sharing options...
PFMaBiSmAd Posted September 18, 2010 Share Posted September 18, 2010 A) You should always post actual code that exhibits the stated symptom so that someone can actually help you (there's no reason to be typing code unless it is in a reply trying to help someone.) B) The symptom you are experiencing is that of a session_start() not working (or not even present) and the $_SESSION variable is a plain program variable and not the actual session array. Are you doing this on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php errors would be reported and displayed to help you find out why it is not working as expected? C) Using global on one of the actual super-global arrays actually causes a new variable by that name to be created inside of the function/class and it is NOT a copy of the actual super-global and does not have the values of the super-global array. D) Don't use global any way as it breaks the black-box nature of functions/classes so that they cannot be reused without keeping track of the main program variables that are hard coded into them. E) "and thus such values should be passed as parameters" Yes, that's the way you should always write functions/classes. Quote Link to comment https://forums.phpfreaks.com/topic/213726-classes/#findComment-1112493 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.