bachx Posted April 11, 2011 Share Posted April 11, 2011 If I want to load some user information from the database into an object/array and share it throughout my application, what's the best approach for this? Here is what I thought of doing: - I can call a function (e.g user_info() ) that will return the user information whenever I need it , but It'll have to run a DB query each time I call it. - Load user information once from the DB, assign it into a global array/object, then call that object whenever I need it. A lot of people recommended against using global variables, but I think performance wise it's better than running a query each time. Are there any better alternatives than the above approaches? Quote Link to comment https://forums.phpfreaks.com/topic/233352-global-variables-yay-or-nay-alternatives-maybe/ Share on other sites More sharing options...
KevinM1 Posted April 11, 2011 Share Posted April 11, 2011 Why not store it in sessions? Quote Link to comment https://forums.phpfreaks.com/topic/233352-global-variables-yay-or-nay-alternatives-maybe/#findComment-1199998 Share on other sites More sharing options...
Adam Posted April 11, 2011 Share Posted April 11, 2011 Are there any better alternatives than the above approaches? Yeah, pass the data into whatever class or function it's needed as a parameter. Quote Link to comment https://forums.phpfreaks.com/topic/233352-global-variables-yay-or-nay-alternatives-maybe/#findComment-1199999 Share on other sites More sharing options...
bachx Posted April 11, 2011 Author Share Posted April 11, 2011 Why not store it in sessions? What advantages do sessions have over global variables? Are there any better alternatives than the above approaches? Yeah, pass the data into whatever class or function it's needed as a parameter. This is probably the best solution, though might be a bit redundant if I have lots of functions. Quote Link to comment https://forums.phpfreaks.com/topic/233352-global-variables-yay-or-nay-alternatives-maybe/#findComment-1200035 Share on other sites More sharing options...
Adam Posted April 11, 2011 Share Posted April 11, 2011 This is probably the best solution, though might be a bit redundant if I have lots of functions. If you have lots of functions requiring the same data - and so presumably are related in what they do - you should consider an object-orientated approach. That way you need only pass it to the constructor method, and you can have it available throughout the object. Quote Link to comment https://forums.phpfreaks.com/topic/233352-global-variables-yay-or-nay-alternatives-maybe/#findComment-1200039 Share on other sites More sharing options...
kickstart Posted April 11, 2011 Share Posted April 11, 2011 Why not store it in sessions? What advantages do sessions have over global variables? They are clearly defined as to what they are and no need to declare them as global in any function that wants to use them without them being passed. Also storing them in session saves having to worry about manually loading and saving them on each page refresh. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/233352-global-variables-yay-or-nay-alternatives-maybe/#findComment-1200049 Share on other sites More sharing options...
KevinM1 Posted April 11, 2011 Share Posted April 11, 2011 Why not store it in sessions? What advantages do sessions have over global variables? I was thinking along the lines of data retrieval, as that was your main concern. Apps live on HTTP requests. Storing your data in sessions will mitigate having to retrieve this data from the db after each request. As far as passing the data into functions, yes, use the argument list. I agree with MrAdam about adopting an OO approach. You can easily encapsulate db data in an object and then store that object in sessions to keep it 'alive' for as long as you need. You also gain the added benefit of having the data object being able to handle its own updating and persistence. Quote Link to comment https://forums.phpfreaks.com/topic/233352-global-variables-yay-or-nay-alternatives-maybe/#findComment-1200056 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.