2

Due to slowness, ( and also for coding reasons ), I know that the use of global variables should be avoided as much as possible. It's twice as slow and it decreases the understanding & tracking of the code flow. But sometimes, you do need them in your functions. Sometimes, they do get very handy.

To some degree constants can be used. But as the name says it all, they are constants after all and therefore, they cannot totally substitute the use of global variables.

If you were to not use global variables, and you do need to use them due to the functionality they offer, what options would you refer to? What utilities are out there in PHP so that a variable can be tapped into within functions, outside of functions, basically anywhere and everywhere. Set it somewhere and read/modify it somewhere else.

As an example, I can throw in the use of session variables as an alternative.

You may find it weird but a session variable can easily do the trick. It actually does the job better than a global variable; I can initialize my session variable right within my functions and in no time, all other parts of my code is able to work with that session variable. Whereas with the global variables, they must be introduced/defined outside the functions scope ( to be exact, at the top of the page ) before they may get used by other sections. Sessions won't suffer from this limitation. However, it is obvious, that Sessions are not the right tool for the goal at hand.

The question is what other ways are out there to tackle the matter of "setting a variable somewhere and read/modify it anywhere else"?

Average Joe
  • 4,521
  • 9
  • 53
  • 81
  • 2
    If you even think you need global variables, and if you're trying to use hacky alternatives to them, you probably aren't coding right. There's almost never a situation where they are needed or even remotely helpful. – Lusitanian Jul 28 '12 at 01:47
  • I agree with @Lusitanian, if you are using globals then you need to re-think the way the you are developing. – eagle12 Jul 28 '12 at 01:51
  • 4
    @Lusitanian, There are plenty of reasons where global variables can come in handy. Some folks choose to put configuration there, or instances of logging classes. I don't think this is so problematic. Are global variables generally over-used? Absolutely. Is it good to keep a lot of things there? Of course not. Are there instances where their usage does more good than harm? You bet. I completely agree that they should only be used in specific cases, but I don't think backlash against ever keeping anything global isn't warranted either. – Brad Jul 28 '12 at 01:51
  • @Brad "almost never" =/= never. Settings are a reasonable enough use. – Lusitanian Jul 28 '12 at 01:57
  • @Lusitanian, Agreed, I was simply trying to present the other side. – Brad Jul 28 '12 at 01:59
  • I agree with Brad. function foo() {global $echo_function_name;if ($echo_function_name){ echo _ _ FUNCTION _ _ ;} Imagine, you got a truck load of functions like that. How can you turn the echoing of all functions on as easy as turning $echo_function_name=TRUE; without the use of global vars? there could be plenty of other uses of global vars. http://codex.wordpress.org/Global_Variables – Average Joe Jul 28 '12 at 01:59
  • @AverageJoe that "echo_function_name" variable should either A) be a constant or B) be encapsulated somehow. – Lusitanian Jul 28 '12 at 02:01
  • Don't sessions require additional pre-and-post-interpretive processing? Sessions are very useful, but I don't understand how they are an escape from the "speed concerns" you speak of. Isn't `$_SESSION` a super global that requires extra processing to build (populating from session store with `session_start`) and then extra processing to write back to the session store with the end of the script processing? – ghbarratt Jul 28 '12 at 02:02
  • @ghbarratt Global variables are clearly faster than sessions for storing data that's meant to be stateless in a stateless manner. Sessions are a form of statefulness between requests which isn't what the OP actually wants. – Lusitanian Jul 28 '12 at 02:03
  • @Lusitanion, you may run into situations if (some_condition) {$echo_function_name=TRUE}else{$echo_function_name=FALSE;}. So constants won't help. Wordpress code base make a good use of globals, all over the place and WP works like a charm. I appreciate your comments, i really do. But the question was not about whether global vars are good or not. – Average Joe Jul 28 '12 at 02:07
  • @Lusitanian Yes, that's what I was thinking as well (speaking to your response to my comment). – ghbarratt Jul 28 '12 at 02:10
  • @ghbarratt & Lusitanian . you guys are right about the session and the speed issue. they can never be faster than global vars. I will edit the original question to take the speed part out. – Average Joe Jul 28 '12 at 02:12
  • "Due to slowness" -> someone correct me if I'm wrong but, good practices aside, I don't think there's any reason to suspect globals are slower than local variables. – Mahn Jul 28 '12 at 03:04

2 Answers2

1

If you do feel the need for global variables you could have a global class that uses the singleton pattern: here is an example of the singleton pattern in PHP http://www.talkphp.com/advanced-php-programming/1304-how-use-singleton-design-pattern.html

Basically the singleton pattern only allows one instance of a class that is shared throughout your application, essentially giving you a global class to store global variables.

1

In answer to your question of "The question is what other ways are out there to tackle the matter of "setting a variable somewhere and read/modify it anywhere else"?"...that's still missing the point: you're defining a global variable. If you're building an application that needs to access variables from anywhere, its design pattern is more than likely inherently flawed.

That being said, to do what may want more properly you can build a "Bag" style class based with set/get functions for, as an example; certain settings. Inject that class wherever you need access to those settings.

Lusitanian
  • 11,012
  • 1
  • 41
  • 38
  • Esentially dependency injection + a dependency container (probably a good idea to add a reference to those two in the answer). I use dependency containers myself, but there are those who think these kind of classes are bad because it's hard to tell what the actual dependencies of each class/function are by only passing the container around. (See: http://stackoverflow.com/a/6034810/1329367) – Mahn Jul 28 '12 at 03:12