Upgrade to PHP 7.4 now

This post is for all that are using CakePHP and anything below PHP 7.4.

In general

The main reason here is that the last version of the PHP 7.x series is the most strict one and prepares you well for the upcoming PHP 8 major and its even more strict behavior.
And I am not even talking about "strict types" mode or param/return types.
Just the general behavior for starters.

Be careful with PHP 8 for the next months, since there will be quite some breaking behavior to watch out for.
Any str_*() method and null instead of a string input can cause now an exception already.

Thus my recommendation: Stick with 7.4 for the time being, it will be supported way into 2022.
None of the PHP 8 features justify such a dramatic jump with potential fallout across your whole application.
But at this point (with PHP 7.2 being EOL now on top) there is no reason to use anything below PHP 7.4 IMO.
You will also improve from the performance improvements.

Libraries and frameworks

As Nikita correctly points out, some frameworks or libraries (used by those frameworks or some of your tools) bump their requirements and minimum PHP versions too aggressively, often without any actual need for it.
There are quite popular but bad players that do that even for fun – very counterproductive to the PHP ecosystem and stability as a whole.
The problem here is that often bugfixes then end up being unreachable for you if you don’t try to stay at least somewhat in the upper range of the current PHP major.
In our current case PHP 7.3 or PHP 7.4 then.

As a generic guideline: Try to stay away from those too aggressive libraries, as they will only create trouble for you.
A wise maintainer will balance the need for new "cool" features with some conservative bumping of minimum versions, and not just jump to PHP 7.4+ or even PHP 8.0+ only right now because of shiny new things that will cause your app to go into the limbo between those requirements. Navigating out of that, replacing libraries where needed, can be a costly undertaking.

Upgrading from CakePHP 2/3

If you are coming from the ORM array world, this will be especially important and useful for you.
So far, there was one big issue in CakePHP apps that I upgraded: They contained silent "dead and broken code" now. Hard to find bugs.

E.g.

echo h($topic->channel['title']);

Even if for some reason the "contain" of channel was not fetched properly, it would silently do nothing and output an empty string.
No report or notice whatsoever on a completely missing relation.
The array access cloaks this in earlier PHP versions.

But with PHP 7.4:

Notice Error: Trying to access array offset on value of type null
In [/…/templates/…/view.php, line 41]

Now we do get useful debug output, for either test run (locally or in CI), or even just in logs on production.
You can now find those and fix them if you didn’t notice them yet.

This also helps to make upgraded apps way less buggy before deploying the app with "silent issues" that you didn’t spot in QA phase.

The upgrade is simple

For nginx this is especially simple.
You just replace

fastcgi_pass    unix:/var/run/php/php7.2-fpm.sock;

with

fastcgi_pass    unix:/var/run/php/php7.4-fpm.sock;

for example, run /etc/init.d/nginx reload and it should be up and running the new minor.

Other issues?

I know that e.g. implode() argument order or a few other PHP internal methods have also been deprecated in parts of their legacy API.
This can also then be adjusted already in an intermediate step before jumping the major for real someday in the future.

Did you spot other issues and helpful strictness that helps you to prepare your app better?
Reach out in the comment section.

0.00 avg. rating (0% score) - 0 votes

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.