What's new in PHP 8.1

Thiemo Kreuz, February 2022

While our Wikimedia servers are stuck with PHP 7.2 for a little longer, I'm very much looking forward to the day we can start using the new features every later PHP version came with. Continuing a series of similar posts here is my personal list of highly anticipated changes and features that are new in PHP 8.1, compared to PHP 8.0. As always in order of personal preference.

TL;DR: Enums. Intersection types with A&B. readonly class properties. never return type. array_is_list(). First-class callables with foo(...).

Update from November 2022: As it turned out the by far biggest impact on MediaWiki was because built-in string functions like trim(), substr(), preg_match(), and many, many more stopped accepting null. This didn't only affect problematic code we are happy to fix. The deprecation also broke a lot of code that used the behavior as a feature. For example, an if ( trim( $input ) === '' ) check worked just fine with null. Luckily we also got convenient features like ?? (since PHP 7.0) and ??= (since PHP 7.4) that made the migration less painful.

While putting this together I also stumbled upon PHP.Watch, a much more detailed resource for exactly what I do here. Written by a single person and going into way more detail than you ever asked for. 👍 Which leaves me with the task of compiling a short version that's focused on the MediaWiki development I mostly care about. Let's go.

Some more things that are most probably mistakes in your code will spit out deprecation warnings now. For example, $array[ 1.5 ] won't silently drop the .5 any more. Internal functions will not silently accept null any more when a parameter is not really nullable. While one might want to classify these as (future) breaking changes, personally I think of these more as issues with my code I want to fix.

PHP 8.1 was released in November 2021, will be actively supported for 2 years and continue to receive security updates until November 2024.

Older posts: