Scout APM helps PHP developers pinpoint N+1 queries, memory leaks & more so you can troubleshoot fast & get back to coding faster. Start your free 14-day trial today.

What's new in PHP 8.1

PHP 8.1 is currently in active development and will be released on November 25, 2021. Keep in mind that this date can still change if the core team, for examples, adds an extra beta release. We already know about new features, performance improvements, changes and deprecations; so let's go through them one by one.

Tired of managing servers? Check out Serverless Visually Explained by Matthieu Napoli and learn how to create scalable PHP applications on AWS.

# New features

As with every release, PHP 8.1 adds some nice new features. Keep in mind that this list will grow over the year.


# Enums RFC

Enums will be added in PHP 8.1! If you're unsure what they can be used for, you can read about them here.

Adding enums would be a significant improvement in PHP, so I for one am very much looking forward seeing enums arrive in PHP 8.1. To give you a quick preview of what they will look like, here's a code sample:

enum Status {
  case Pending;
  case Active;
  case Archived;
}

And this is how they will be used:

class Post
{
    public function __construct(
        private Status $status = Status::Pending;
    ) {}

    public function setStatus(Status $status): void
    {
        // …
    }
}

$post->setStatus(Status::Active);

You can find an in-depth analysis of how to use enums in this post.


# Fibers RFC

Fibers — aka "green threads" — are a low level mechanism to manage parallelism. You probably won't use them directly in your applications, but frameworks like Amphp and ReactPHP will make extensive use of them.

Here's a simple example of using fibers:

$fiber = new Fiber(function (): void {
    $valueAfterResuming = Fiber::suspend('after suspending');
    
    // … 
});
 
$valueAfterSuspending = $fiber->start();
 
$fiber->resume('after resuming');

# Performance improvements PR

Dmitry Stogov has added some improvements to opcache, he calls it "inheritance cache". This feature allows links between classes to be cached, much like linked classes can be preloaded as of PHP 7.4.

Dmitry reports between a 5% and 8% performance increase thanks to this change, a nice little detail to look out for in PHP 8.1.


# Array unpacking with string keys RFC

Array unpacking was already allowed in PHP 7.4, but it only worked with numeric keys. The reason string keys weren't supported before is because there wasn't any consensus on how to merge array duplicates. The RFC cleanly solves this by following the semantics of array_merge:

$array1 = ["a" => 1];

$array2 = ["b" => 2];

$array = ["a" => 0, ...$array1, ...$array2];

var_dump($array); // ["a" => 1, "b" => 2]

# New array_is_list function RFC

You've probably had to deal with this once in a while: determine if an array's keys are in numerical order, starting from index 0. Just like json_encode decides whether an array should be encoded as an array or object.

PHP 8.1 adds a built-in function to determine whether an array is a list with those semantics, or not:

$list = ["a", "b", "c"];

array_is_list($list); // true

$notAList = [1 => "a", 2 => "b", 3 => "c"];

array_is_list($notAList); // false

$alsoNotAList = ["a" => "a", "b" => "b", "c" => "c"];

array_is_list($alsoNotAList); // false

Do you want to stay up-to-date about PHP 8.1's development? Subscribe to my newsletter and receive occasional updates:


# Explicit octal integer literal notation RFC

You can now use 0o and 0O to denote octal numbers. The previous notation by prefixing a number with 0 still works as well.

016 === 0o16; // true
016 === 0O16; // true

# Breaking changes

While PHP 8.1 is a minor version, there will be some changes that might technically be a breaking change, and deprecations as well. Let's discuss them one by one.


# Restrict $GLOBALS usage RFC

A small change to how $GLOBALS is used will have a significant impact on the performance of all array operations. Nikita does a fine job explaining the problem and solution in the RFC. The change means that some edge cases aren't possible to do any more with $GLOBALS. "What is no longer supported are writes to $GLOBALS taken as a whole. All the following will generate a compile-time error":

$GLOBALS = [];
$GLOBALS += [];
$GLOBALS =& $x;
$x =& $GLOBALS;
unset($GLOBALS);

On top of that, passing $GLOBALS by reference will generate a runtime error:

by_ref($GLOBALS); // Run-time error

Nikita analysed the top 2000 packages on Packagist, and only found 23 cases that will be affected by this change. We can conclude the impact of this — technically breaking — change will be low, which is why internals decided to add it in PHP 8.1. Remember that most of us will win by this change, given the positive performance impact it has everywhere in our code.


# Resource to object migrations

These changes are part of the long-term vision to convert all resources to dedicated objects. You can read more about it here.

Fileinfo functions with finfo objects

Functions like finfo_file and finfo_open used to accept and return resources. As of PHP 8.1, they work with finfo objects.

IMAP functions with IMAPConnection objects

Just like the fileinfo change, IMAP functions like imap_body and imap_open no longer work with resources


# Deprecate passing null to non-nullable arguments of internal functions RFC

This change is simple: internal functions currently accept null for arguments that are non-nullable, this RFC deprecates that behaviour. For example, this is currently possible:

str_contains("string", null);

In PHP 8.1, these kinds of errors will throw a deprecation warning, in PHP 9 they will be converted to type errors.


# Other small changes

With every release, there's a bunch of very minor changes to the language. All of them are listed in the UPGRADING guide on GitHub, make sure to check it out if you want to know every little detail.

Here's a summary of the most significant changes:


That's it for now, keep in mind I'll regularly update this post during the year, so make sure to subscribe if you want to be kept in the loop. Are you excited for PHP 8.1? Let me know on Twitter!

Noticed a tpyo? You can submit a PR to fix it. If you want to stay up to date about what's happening on this blog, you can follow me on Twitter or subscribe to my newsletter: