Jonob Posted April 20, 2009 Share Posted April 20, 2009 Just getting my head around using $_SESSION to store variables. Lets assume that I am storing something like $_SESSION['company'] and I need to access this variable in all the functions in a file. Assume that my file looks as follows: <?php class test { function foo() { //Do something here } function bar() { //Do something else here } } ?> Is there an easy way for me to do something like $company = $_SESSION['company'] at the top of the php file (or class), and then use $company_id in each function? i.e. can I use $company_id as a variable in each function, without having to redeclare it inside each function?? Link to comment https://forums.phpfreaks.com/topic/154865-solved-_session-variable/ Share on other sites More sharing options...
mrMarcus Posted April 20, 2009 Share Posted April 20, 2009 have you tried it? Link to comment https://forums.phpfreaks.com/topic/154865-solved-_session-variable/#findComment-814482 Share on other sites More sharing options...
PFMaBiSmAd Posted April 20, 2009 Share Posted April 20, 2009 $_SESSION['company'] is a superglobal variable (available in all scopes.) Just use $_SESSION['company'] any place you need it. $company = $_SESSION['company'] is just making a copy in a program variable that has the scope of wherever that statement is at and is just wasting memory and processing time. Link to comment https://forums.phpfreaks.com/topic/154865-solved-_session-variable/#findComment-814487 Share on other sites More sharing options...
Jonob Posted April 20, 2009 Author Share Posted April 20, 2009 $_SESSION['company'] is a superglobal variable (available in all scopes.) Just use $_SESSION['company'] any place you need it. $company = $_SESSION['company'] is just making a copy in a program variable that has the scope of wherever that statement is at and is just wasting memory and processing time. OK, makes sense. Thank you. Link to comment https://forums.phpfreaks.com/topic/154865-solved-_session-variable/#findComment-814492 Share on other sites More sharing options...
jackpf Posted April 20, 2009 Share Posted April 20, 2009 You do need to declare the variable as global though. Link to comment https://forums.phpfreaks.com/topic/154865-solved-_session-variable/#findComment-814493 Share on other sites More sharing options...
PFMaBiSmAd Posted April 20, 2009 Share Posted April 20, 2009 Not a session variable and using the global keyword inside of functions is another poor programming practice that php added to get around the fact that early versions of php did not support classes (which is what you use when you have closely related variables and functions that use those variables.) Link to comment https://forums.phpfreaks.com/topic/154865-solved-_session-variable/#findComment-814501 Share on other sites More sharing options...
jackpf Posted April 20, 2009 Share Posted April 20, 2009 No, I mean if he assigns a variable to the value of a session, he will need to make that variable global within the function. And yeah, that's true, but he said he wanted to declare at the top of the script. If so, then it will have to be made global at some point. Link to comment https://forums.phpfreaks.com/topic/154865-solved-_session-variable/#findComment-814527 Share on other sites More sharing options...
PFMaBiSmAd Posted April 20, 2009 Share Posted April 20, 2009 $_SESSION variables are already global. Nothing more needs to be done to access them inside of functions and doing $company = $_SESSION['company'] is wasteful, unnecessary, and introduces a limit on the scope of the variable that would need to be worked around. Link to comment https://forums.phpfreaks.com/topic/154865-solved-_session-variable/#findComment-814532 Share on other sites More sharing options...
jackpf Posted April 20, 2009 Share Posted April 20, 2009 Yeah, that's what I mean. I thought he wanted to assign a variable to a $_SESSION index. In which case it would need to be made global. Link to comment https://forums.phpfreaks.com/topic/154865-solved-_session-variable/#findComment-814557 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.