Jump to content

Global variables. Yay or nay? Alternatives maybe?


bachx

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.