Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to Globals

Tags:

php

wordpress

I have a set of variables that need to be accessible across my site. While I could declare them in my header.php file, I would prefer to define them in functions.php, or just not in a template file (view). Everything I read says DON'T USE GLOBAL's...fine. But then what is the best way define these variables?

Here's what I'm looking to define:

 global $favorites_array;
 global $user;
 global $user_id;
 global $user_name;
 global $uri;

 $user = wp_get_current_user();
 $user_name = $user->user_login;
 $user_id = $user->ID;
 $user_id = get_current_user_id();
 $uri = $_SERVER['REQUEST_URI'];
like image 221
ok1ha Avatar asked Mar 19 '23 10:03

ok1ha


1 Answers

The common advice you've found against using globals (or avoiding them whenever possible) is generally good advice, and you are correct to look first for alternatives rather than resorting to using them. The most common alternative is to pass the values into your functions as parameters.

A lot of functionality of WordPress is already dependent on global variables, and looking into the source code it would seem that the function wp_get_current_user() is actually setting a global variable called $current_user.

Multiple calls to wp_get_current_user() will immediately return if the global $current_user is already populated:

if ( isset( $current_user ) && ( $current_user instanceof WP_User ) && ( $id == $current_user->ID ) )
    return $current_user;

So looking over the list of variables you intended to set, I see no compelling need for any container variables on any of them. My advice would be to opt for explicit readability in your code wherever possible. Since WordPress supplies the function wp_get_current_user() whose purpose is to get that user, instead of creating a global $user and passing that around, make a call to the available function. It's true that you cannot, for example, use the function call inside a string interpolation:

// Can't do this:
echo "The user is {wp_get_current_user()->user_login}";

... and so in cases where you had otherwise intended to create a global variable for the user's login, set a local on one in your function.

function your_func() {
  $login = wp_get_current_user()->user_login;
  echo "The user is $login";
}

As for setting a variable like $user_id which serves as a container for the ID property of the user object, since ID is indeed a property of the user object, it makes good logical sense to maintain that relationship in your code. Though this may be faster to type:

echo $user_id;

This makes explicit the source of the variable:

echo wp_get_current_user()->ID;
// or even better, using the API function
echo wp_get_current_user_id();

Finally, with regard to the $_SERVER array, it is already a PHP superglobal array, available in all scopes without any extra effort needed. So instead of abstracting out one key from it in the form of:

$uri = $_SERVER['REQUEST_URI'];

I would opt to just make use of $_SERVER['REQUEST_URI'] directly in your own functions. If you find you need to use it several times in one function, for example, then feel free to create a local variable in the function:

function uses_uri() {
  $uri = $_SERVER['REQUEST_URI'];
  echo "I like to say $uri over and over $uri $uri $uri";
}

But again, if you are using the superglobal value, your code will be more readable if it is used directly or the local variable is defined near where it is to be used (as opposed to a global variable defined in a separate include file whose value must be traced back to be understood).

Because I am not terribly familiar with WordPress' API, I cannot say for certain if it provides an API function to retrieve the request URI. I would not be surprised however, if such a function already exists and is conventionally used in WP code.

like image 147
Michael Berkowski Avatar answered Mar 28 '23 19:03

Michael Berkowski