Quantcast
Channel: Press Up
Viewing all articles
Browse latest Browse all 101

Globals and Variable Scope in PHP and WordPress

$
0
0

One of the most maligned features of WordPress is the rampant use of “globals”. There are huge discussions about why heavy reliance on global scoped variables is a bad design decision, but that debate is beyond the scope of what I want to cover here. What I want to explain is instead what variable scope means, and why and how global variables are used in WordPress.

As to the conflict, I’ll just say that there are lots or reasons that the programming and PHP community as a whole has moved away from global variables but there are a large number of reasons that the WordPress project and community have not. More important than that discussion is understanding its meaning and implications, so lets get started.

You Keep Using that Word… What is “scope”?

What this means is that any given unit of PHP will either have access to variables in the global scope, or only have access to the variables declared inside the currently executing function.

It’s a popular brand of mouthwash in the United States. But it’s also, and more importantly, about what’s relevant and visible to who and where. Programmers use the phrase “out of scope” when talking about activities or ideas that aren’t currently relevant to what they’re working on, and that’s got a lot to do with variable scope.

Basically, variables are accessible only within in a certain scope. This is done it various ways, and based on various rules in various languages, but its purpose is to prevent the problem that one part of a program may set $i to 5, and while it’s waiting to use that value, and another part sets it to 981235792 or “cucumber.” If they’re both using the same $i, one of those parts of the program is highly likely to malfunction. Essentially, variables are given scope to prevent programs (and programmers’ heads) from exploding. The specific rules that control what variables are accessible in which places vary from programming language to programming language. What’s relevant to someone interested in WordPress and PHP is the specific way it works in that context.

PHP as a language uses function scoping. What this means is that any given unit of PHP will either have access to variables in the global scope, or only have access to the variables declared inside the currently executing function. There are some specific complications to this explanation that we’re going to skip for this article, but that’s essentially the story.

Playing with Scope in PHP

In this section I just want to run you through the basics of what all this scope stuff looks like in some basic PHP.

$global = "I am a global variable because I wasn't defined inside a function.";
my_global_function();
my_nonglobal_function($global);

function my_global_function() {
	global $global;
	echo $global;
}

function my_nonglobal_function($global) {
	echo $global;
}

Alright, as we said in the last section, there are really only two relevant scopes in PHP for variables you create, global and function. (There is a third type of scope, but if you want to know more about superglobals you’ll have to wait.) Because we’re declaring the variable $global outside of a function, it will automatically have global state.

A variable that’s inside of a function can be accessed in two ways. First, it can be accessed in PHP by using the global keyword. This is what our first called function, my_global_function, does. By asserting that it wants the global variable $global, the echo — which is just PHP vocabulary for “throw onto the screen” — can output the string that we declared in the first line of the example. If we’d skipped the global line, we’d — depending on settings — either see a PHP error, or nothing on the screen at all. Neither is what we wanted.

The other way that we can use the pre-declared variable $global is that we can pass it into the function as a parameter. This allows us to be a little more certain about what variable we’re truly getting in the function, because it’s almost like a literal hand-off, and it allows us to to skip straight to the echo $global without a stop off to import the variable.

As you can see, these are two functions that both accomplish the same thing. You’ll see both methods occur in various places in various PHP projects. Use of global has fallen out of favor in the PHP community because of the risk — hard to picture in our example, but not impossible, that before our function got around to access $global it could easily have been changed to anything and we’d be left having made a wrong assumption about it and quite possibly causing errors. The method of passing the relevant variables into the function when it is called is thus preferred in most modern projects that don’t worry about backwards-compatibility as much as WordPress.

Scope in WordPress

As I said at the top, WordPress relies heavily on global variables. (Here’s the list of all its globals.) As such, it is exceptionally common that you’ll see a function start with a line like

global $post

Hopefully by now you’ve got a pretty good idea of what this is doing: importing in the entire global $post variable — which functions like the_title() rely on — to local scope so it can act on it. This is so common you’ll see people forgot the need to declare the global or add the line at the top of their function when it’s not really necessarily. So be on the lookout, understand what global means, when you need it, and what it’s for.

Where you can avoid it, it’s generally considered a better idea to not add things to the global scope. Given a choice between passing in a variable to the new function using it as a parameter, or adding your variable to the global scope, its best practice to do the former. But globals don’t seem to be going anywhere soon in WordPress, so you’ve earned a valuable power by understanding them.

The post Globals and Variable Scope in PHP and WordPress appeared first on Press Up.


Viewing all articles
Browse latest Browse all 101

Trending Articles