Migrating to php 8.x

Many websites are still running on older versions of PHP, usually PHP 7.4 or even older, and this can represent a security and performance risk for the website and therefore is always recommended to keep with the current version of PHP. For example, PHP 7.4 is already End Of Life to the 18 Nov 2021, this means that there are no security or code updates whatsoever.  Someone could say that he do not care, which is an option, and since the website that runs the older version of PHP is not ciritical, even vulnurable security is acceptable. However, what if you would like to keep with Joomla 4? The recomended PHP version is 8.0 although the minimum still supported is PHP 7.2.5. This give us one more reason to not upgrade since you can still use the benefit of Joomla 4 on older PHP version but newer PHP means as well a performance boost and supported new features. Hence, it is more than recommended to move and upgrade the PHP to ensure the best exprience. Just look on the release note:

PHP 8.1 is a major update of the PHP language that brings better performance, better syntax, and improved type safety. It also contains new features and optimization improvements.

How to do so? In these days it is simply done through the CPanel of your hosting. Usually, hosting companies are supporting a various options of PHP but you would be most likely looking for PHP 8.1 or PHP 8.2. With PHP 8.1 you should be safe with security updates untill the end of 2024 and thus a lot of time to think what to do next. Anyway, back to the Control Panel of your host, just browse to these settings (every hosting company has it a bit different) and change the PHP. If you are lucky, you are done and you can verify your PHP version in the CPanel or Administrator backend of your Joomla websire under  System-System Informaiton. However, what if I am not the lucky one and I will get an error message on the FrontEnd?

With an error message you have two options. By Joomla, and I suppose you are running the Joomla 3.x, the core will be compatible, however extensions can be a problem. First thing to do is to revert back to the old version of PHP, like PHP 7.4 and try to update the extensions. No update? No problem at this moment since we will do some Debug to find the root cause.

Update the PHP settings once more, again to PHP 8.1 and go to your Front End. If you see an error, okay, stay steady and read the next few lines. Login to your Back End which would be most likely working and turn on Debug under System-Global Configuration-System and Debug System. Refresh the Front End and you will see, next to your error, some more informaiton about where it happens. No Back End? Still no problem. Go via FTP to your Joomla toor directory, open "configuration.php" in any text editor and change line public $debug = false; to TRUE. Well, debug is on and we see a bit more about what is happening.

configuration.php
#Find line
$debug = false; #and change false to true

asdasd

Non-static method mod_resliderHelper::getImages() cannot be called statically

Looking on the output from debug you will see bunch of stuff and something is causing the error and something is just the subsequent result of this error. Lets focus on the first line. Now, the first line points us to a module located JROOT\modules\mod_reslider\mod_reslider.php:11 and specific php line, which is 10. So what is going on, again log via FTP to your website and locate the php file cause the error. At this moment you have two options, look within the PHP documetnation to find the difference between PHP 7 and PHP 8, or ask someone online. Anyway, you will find that the addressed line needs to be modified because it was written following an older PHP standard, well what a supprise, right?

$listofimages = mod_resliderHelper::getImages($params);
#change to
$listofimages = (new mod_resliderHelper)->getImages($params);
#
#you will notice that after the first error a second will appear
#
mod_resliderHelper::load_jquery($params);
#change to
(new mod_resliderHelper)->load_jquery($params);

After some fiddling and fixing these errors, I had to fix another module that was a copy of the original one with some additional changes to adapt to 3D-Sphere. The errors were the same, and refered to Non-static method cannot be called statically. Therefore the fix was easy to be made, as above, but to keep a track of this I have pasted the code below as well with the changes I have made.

$listofnews = mod_3dnewsHelper::getNews($params);
#change to
$listofnews = (new mod_3dnewsHelper)->getNews($params);
#
$listofcontrols = mod_3dnewsHelper::getControls($params);
#change to
$listofcontrols = (new mod_3dnewsHelper)->getControls($params);

Fixing these errors was simple but straight after that I got version_compare(): Argument #3 ($operator) must be a valid comparison operator that came out from the YJSG framework that I was still using. Now, the framework was great and I was quite unhappy to see that the development stopped but this is happening from time to time, right? The problem is that if you are used to something and this will happen you need to either fix the error or move to another framework. This process is time-consuming and can break your site and vision. Still, right now we are talking about PHP errors, this is possible to be fixed much easier than Joomla 4 errors. Let's have a look at them.

version_compare(): Argument #3 ($operator) must be a valid comparison operator
#located in
JROOT\plugins\system\yjsg\includes\yjsgcore\classes\yjsg.class.php:287

The error message already points to a valid comparison operator and therefore let's have a look at how these operators changed in PHP 8.1. After a brief search, you will find that '=>' was replaced by '>='. This seems like a problem that shouldn't be a problem to fix, right? Ok, so let's open the PHP file where the error appears and mass-replace these operators as shown below. The line only verifies the Joomla version and make sure you are using at least Joomla 3.2.

if (version_compare(JVERSION, '3.2', '=>')) {
#change to
if (version_compare(JVERSION, '3.2', '>=')) {

Voila, the site is back on-line! Mind the missing image, I am running it locally and some files are just still on the server but as you can see, moving to a newer PHP version was not so difficult. There were some errors, yes, but using the DEBUG function and some documentation fixed the issue. Of course, there could be more hidden errors worth fixing and some will appear later on but to make the site on-line until you will manage to move to a different framework, well, why not.

Popular