jimmo Posted May 11, 2007 Share Posted May 11, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/50903-diffrent-way-of-accessing-variables-globally/ Share on other sites More sharing options...
redbullmarky Posted May 11, 2007 Share Posted May 11, 2007 have a look here for one possibility: http://www.phpfreaks.com/forums/index.php/topic,139997.15.html Quote Link to comment https://forums.phpfreaks.com/topic/50903-diffrent-way-of-accessing-variables-globally/#findComment-250402 Share on other sites More sharing options...
emehrkay Posted May 12, 2007 Share Posted May 12, 2007 Im lost as to why not use the SESSION super global...? and to limit your session definitions, i would suggest using bitwise operators. Quote Link to comment https://forums.phpfreaks.com/topic/50903-diffrent-way-of-accessing-variables-globally/#findComment-251474 Share on other sites More sharing options...
Nameless12 Posted May 13, 2007 Share Posted May 13, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/50903-diffrent-way-of-accessing-variables-globally/#findComment-251841 Share on other sites More sharing options...
jimmo Posted May 14, 2007 Author Share Posted May 14, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/50903-diffrent-way-of-accessing-variables-globally/#findComment-252574 Share on other sites More sharing options...
jimmo Posted May 14, 2007 Author Share Posted May 14, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/50903-diffrent-way-of-accessing-variables-globally/#findComment-252576 Share on other sites More sharing options...
johnrcornell Posted May 19, 2007 Share Posted May 19, 2007 I know, put all the data you want in an array, serialize, base64 encode it, then put its value into a hidden input field on your form. Then on the next page do an extract($_REQUEST); LMAO DO NOT DO THAT Quote Link to comment https://forums.phpfreaks.com/topic/50903-diffrent-way-of-accessing-variables-globally/#findComment-256750 Share on other sites More sharing options...
jimmo Posted May 19, 2007 Author Share Posted May 19, 2007 ???? Quote Link to comment https://forums.phpfreaks.com/topic/50903-diffrent-way-of-accessing-variables-globally/#findComment-256874 Share on other sites More sharing options...
Mastodont Posted May 24, 2007 Share Posted May 24, 2007 as everything in $_SESSION gets serialized and unserialized Yes, sessions are file-based. $GLOBAL array not? Quote Link to comment https://forums.phpfreaks.com/topic/50903-diffrent-way-of-accessing-variables-globally/#findComment-260869 Share on other sites More sharing options...
448191 Posted May 24, 2007 Share Posted May 24, 2007 as everything in $_SESSION gets serialized and unserialized Yes, sessions are file-based. $GLOBAL array not? You are confused. Quote Link to comment https://forums.phpfreaks.com/topic/50903-diffrent-way-of-accessing-variables-globally/#findComment-260899 Share on other sites More sharing options...
Mastodont Posted May 24, 2007 Share Posted May 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/50903-diffrent-way-of-accessing-variables-globally/#findComment-260996 Share on other sites More sharing options...
448191 Posted May 24, 2007 Share Posted May 24, 2007 Well, as newbie I'm entitled to be confused. Good point. 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. Quote Link to comment https://forums.phpfreaks.com/topic/50903-diffrent-way-of-accessing-variables-globally/#findComment-261008 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.