Broken Joomla Extension After Updating to PHP 7.2? Read This!

Note: This post is somehow technical. Some PHP knowledge is required to understand it.

After updating PHP to 7.2 on the server powering the Joomla website of a major client, we noticed that one of the modules became broken. The module was supposed to display some images with a specific size, and, while the images were being displayed, the size was not correct, and some of the images were overlapping (because the layout assumed that all the images had the same, specific width and height).

We investigated the issue, and we were able to determine that it was caused by this line:

$$arrSingleItemPreText[0] = $arrSingleItemPreText[1];

The above line contains $$, which PHP refers to as variable variable. So, what is a variable variable?

Let us explain it with an example… Let’s say that you have the following PHP code:

$myVariable = "itoctopus";
$$myVariable = "Joomla Experts";

The first line is obvious. The second line is translated the following way:

$itoctopus = "Joomla Experts";

As you can see, we replaced $myVariable to its value in the first line, and we left the first $ sign. So, $$myVariable became $itoctopus, which means that $itoctopus will have the value of “Joomla Experts”.

Now, going back to the above line we were having a problem with, we examined it thoroughly, and we were sure that there was nothing wrong with it. It was just another variable variable, so why is that a problem after updating to PHP 7.2? Has PHP suddenly decided to stop interpreting variable variables? We researched the issue, and, as we had expected, the root cause turned out be something different…

It was the interpretation of the line that was different. In versions prior to PHP 7, PHP had a mixed interpretation of variables, in most cases it interpreted them left to right, but, in some cases, it interpreted them right to left. In PHP 7, for consistency reasons, variable interpretation is strictly left to right (see the section “Changes to variable handling” on the official PHP 7 backward incompatibility page). So, in PHP 5, the above line was interpreted as:

${$arrSingleItemPreText[0]} = $arrSingleItemPreText[1];

In PHP 7, however, it was interpreted the following way:

($$arrSingleItemPreText)[0] = $arrSingleItemPreText[1];

The first line above (the PHP 5 interpretation) means that if the value of $arrSingleItemPreText[0] is “myVariable”, then $myVariable will be set to $arrSingleItemPreText[1]. The second line (the PHP 7 interpretation) will try to find a value for the variable $arrSingleItemPreText (which is an array), and then set the variable made out of that value to $arrSingleItemPreText[1]. This is a completely different thing, and it will certainly fail.

So, how did we solve the problem?

We solved the problem by replacing the problematic line with this one:

${$arrSingleItemPreText[0]} = $arrSingleItemPreText[1];

This forced PHP 7 to emulate the same behavior of PHP 5 for our particular variable variable.

Is the Joomla 3.8.3 core affected by PHP 7.2 update?

The short answer is no. The core (in the absolute majority of cases) is not broken by an update to PHP 7.2 . Having said that, there are quite a few extensions that are not compatible with PHP 7.2.

We hope that you found this post useful. If your Joomla 3.8.3 site (ideally, you should only update to PHP 7.2 if you’re running the latest version of Joomla) gets broken after updating to PHP 7.2, then try disabling your extensions one by one (plugins, modules, switch to a core template) in order to locate the problematic extension. If you need help with that, then please contact us. We will solve you problem swiftly, cleanly, and affordably.

No comments yet.

Leave a comment