Migrating From PHP 7.4 to PHP 8.0

Victor Todoran
3 min readMar 18, 2022

--

An image of migrating birds in a sunset sky
Photo by Barth Bailey on Unsplash

Ever since the release of PHP 8.0 all I’ve been hearing is constructor property promotion. It’s a neat little trick and I like it, though it wasn’t of much help when I started work on PHP 8 compatibility.

This article is a high level overview meant to show you some of the things you need to look into before you can upgrade to PHP 8 and to encourage you to research the topic on your own.

It’s worth noting that at least half of your migration efforts will be directed towards updating external packages and dependencies. You’ll find nothing about that in this article and that is by design, mainly because this subject is very project-specific.

Instead, what you can expect is some of the backwards incompatible changes that come with PHP 8.

Before we dive in, I just want to remind you that PHP 7.4 is supported until November 28, 2022, which is a date that feels dangerously close.

String to number comparisons

Previous to PHP 8 non-strict comparisons between strings and numbers were made by casting the string to a number and then comparing the values.
Now is vice versa, the number is cast to a string and then the values are compared.
Here is a table that represents the dire implications of this change:

source: official docs

Consider this to be one extra reason (if you needed one) to use strict comparisons and strict typing. Note that the behavior is not limited to equals checks, it’s the same for greater than and less than checks as well.

Example of comparison between string and int

Error Handling

If you have a custom error handler in your project, you might want to check it.
As per the documentation, error handlers that expect error_reporting to be 0 when @ is used should be adjusted to use a mask check instead.

source: official docs

Sorting

uasort and usort are now stable, which means that equal elements retain their original order, whereas previously the order of equal elements was undefined.
If you’ve written custom logic that makes the result of these methods stable or if your code is tightly coupled to undefined behavior, it might be time for some refactoring.

Float to string casting is now locale agnostic

source: official docs

Resource to object migration

A bunch of low level methods (e.g curl_init or xml_parser_create) now return a specific object instead of a resource.
The complete list of methods and their return types can be found here.
If you are using is_resource to validate the success of these functions you will need to change that.

PDO

The default error handling mode has been changed from “silent” to “exception”.
Which anyway is the error handling mode that everyone is using.
It is, right? Please tell me that it is.

Division by zero

Division by zero now throws an exception, which might come in handy.

Bugs

New versions are bound to solve some bugs. The problem is that a bug does not always result in an error/exception, sometimes a bug means that an error is silenced or an exception is not thrown.

If you bind more or fewer parameters on a PDOStatement your code will break with the following message:

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens.

However, because of this bug in previous versions of PHP, if the query does not have any placeholder, validation is skipped all together.

This was fixed in PHP 8, and now the exception is thrown as expected even if the query does not have any placeholders.

The lesson learned is that you might have unstable code that is running because of flaws in PHP itself, after you upgrade this might become a problem.

In the hopes that I’ve made you curious about the implications of upgrading to PHP 8, here is a list of all the backwards incompatible changes.

Thanks for reading!

Disclaimer: Consider this to be a living document, which means it’s subject to undocumented changes and it might even die in the future.

--

--

Victor Todoran

I write, read and think about software and its users for a living