Jump to content

diffrent way of accessing variables globally


jimmo

Recommended Posts

Hi All!

 

I have a number of variables that I need to drag around my application and I am trying to decided what is the "best" way. For the most part these are configuration variables, defining things like directories, default values when not otherwise defined and so forth. The application is object based and I have a base class where I could store values I need. Not every variable is needed in every class or in every function.

 

I have also thinking about using the $GLOBAL array, as well simply defining the variables then accessing then as "global $var" when I need to. There are a number of places where I can simply pass the given value to a function.

 

I have been googling for the past couple of days looking for a discussion/comparison of the various methods, but I found nothing really definitive that discusses the pros and cons of the various. So, the basic question is when to use each method and when not to.

 

I am grateful for any input (including simply kicking my backside and telling me where it has already been discussed).

 

Regards,

 

jimmo

Link to comment
Share on other sites

use constants to define directories. Constants are not bound by scope so you can use them anywhere.

You can use globals or super globals also, dont use $_SESSON as everything in $_SESSION gets serialized and unserialized.

You can also use static properties on classes globally.

Another approach is to have a static variable inside a function, because it is static it will persist between calls.

 

Link to comment
Share on other sites

Im lost as to why not use the SESSION super global...? and to limit your session definitions, i would suggest using bitwise operators.

 

Wouldn't that simply be calling the rose by a different name? Naturally the SESSION global is only valid for the session, but there is still the problem of having "everything" in that array. So the problem exists, but with  a different name.

 

What do you mean by "limit your session definitions, i would suggest using bitwise operators"? I certainly know what bitwise operators are, but I am lost as to what bitwise operators have to do with global variables in this context.

Link to comment
Share on other sites

use constants to define directories. Constants are not bound by scope so you can use them anywhere. You can use globals or super globals also, dont use $_SESSON as everything in $_SESSION gets serialized and unserialized. You can also use static properties on classes globally. Another approach is to have a static variable inside a function, because it is static it will persist between calls.

 

It definitely makes sense to use constants for the directories. 

 

I was unaware of the problem with serialization of the $_SESSON super global. Thanks for the tip.

 

At this point, I am not sure if static variable make sense in my application as I cannot think of any cases where a give function is called more than once per page, or if it is, whether there are variable I need to keep between calls. However, it is definitely something to think about.

 

However, I still don't have a definitive answer about when to use each and when.

Link to comment
Share on other sites

Well, as newbie I'm entitled to be confused. Manual says:

 

all data related to a particular session will be stored in a file in the directory specified by the session.save_path

 

So $_SESSIONs are stored in file, right? And I ask a question, where are stored $GLOBALS? In memory or files? Thanks.

 

Link to comment
Share on other sites

Well, as newbie I'm entitled to be confused.

 

Good point.  :D

 

Manual says:

 

all data related to a particular session will be stored in a file in the directory specified by the session.save_path

 

So $_SESSIONs are stored in file, right?

 

Not nessecarily. First off, you have to understand that what is commonly referred to as 'sessions' among php developers are not actually sessions. People usually use the term analogously with php's build-in session management system. The $_SESSION superglobal is part of the interface to this system, as are the session_* functions. By default, this build in system uses files to have data persist across requests. But, you can create your own session management system (in which case you'll also use your own interface), or you can create a customized 'save handler', which enables you to use a different persistence medium (a RDBMS for example), while still utilizing the default interface. Also you could use the mm extension in which case session data is stored in memory instead of files. There is more to it, but let's keep it at this for now.

 

And I ask a question, where are stored $GLOBALS? In memory or files? Thanks.

 

Regular global variables are kept in memory for the duration of script execution.

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.