PHP 8.1 will be released on November 25, 2021. We already know all new features, performance improvements, changes and deprecations so let’s go through them one by one.

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. 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);

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');

If you want to read some more about fibers, what they can and can’t do, you can read this post.

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]

Readonly properties RFC

Class properties can be marked as readonly, meaning they can only be written once.

class PostData {
    public function __construct(
        public readonly string $title,
        public readonly DateTimeImmutable $date,
    ) {}
}

Trying to change a readonly property after it has been initialized will result in an error:

$post = new Post('Title', /* … */);

$post->title = 'Other';

Error: Cannot modify readonly property Post::$title

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 and the small deprecations RFC, make sure to check it out if you want to know every little detail.