Home > other >  Dealing with PHP 8.1 warning for undefined array key
Dealing with PHP 8.1 warning for undefined array key

Time:02-08

We are upgrading to PHP 8.1. A new feature is that an undefined array key throws a warning.

Unfortunately this interferes with the ability to easily use associative arrays such as $_SESSION variables. I understand the virtues of predefining variables, and am not looking for a discussion on those virtues. The idea of the associative array is that you can add things easily to the session and everything not so assigned is evaluated as null. But now it also throws a warning; something has to be done to deal with that.

Here is an example of some code:

$_SESSION['is_condition'] = true;

In another place in the code, the following occurs

if ($_SESSION['is_condition']) ...

If this occurs in a context where the 'is_condition' session variable has not been defined, the desired result of evaluating its value as null is what we want. But now something else has to be done to deal with the possibility that it is undefined.

There are several approaches to dealing with this:

  1. Pre-define all session variables having the value of null. Seems like it is not the spirit of associative arrays. Now every script has to invoke a lengthy set of code.

  2. Use the null coalesce operator whenever the value of an associative array element is required. This is an ugly requirement to place many, many additional operators throughout the code base.

  3. Alter our custom error handling functions to ignore the undefined array key error. A very bad idea to suppress warnings, and adds overhead.

None of these approaches is very desirable.

A theoretical way to solve this would be an array initialization statement that assigns all possible associative keys to null. I don't know of any such thing.

My question is whether there is some other approach that I am missing, such as a parameter that suppresses this specific warning only.

CodePudding user response:

My suggestion would be to write an abstraction over the session mechanism that deals with this problem. I'd opt for a class, but even a simple function could work:

function getValueFromSession(string $key, mixed $defaultValue = null) : mixed
{
   return isset($_SESSION[$key]) ? $_SESSION[$key] : $defaultValue;
}

This has the added benefit of allowing you to change your underlying session storage (e.g. to Redis) without having to change every single instance of session value access.

CodePudding user response:

You are assuming that $_SESSION['whatever'] is always going to be available. You can wrap all your conditionals with isset like so:

if (isset($_SESSION['is_condition'])) {
//Do Whatever
}

Or you can do something unweildy like putting this at the top of your file.

if (isset($_SESSION['is_condition'])) {
//Do something
} else {
$_SESSION['is_condition'] = NULL;
} 

From then on anytime you check the session for that key it will at least be set to something and wont throw a warning or notice. It's ugly but it will work.

  •  Tags:  
  • Related