
What’s new in PHP 8.1
There will be an 8.1 release on November 25, 2021, and is currently in development. The release date may still change if the core team adds an extra beta release, for example. Several new features, improvements, changes, and deprecations are already known, so let’s go over each one in detail.
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
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 to 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);
Readonly properties
Class properties can be marked as read-only, meaning they can only be written once.
class PostData {
public function __construct(
public readonly string $title,
public readonly DateTimeImmutable $date,
) {}
}
Trying to change a read-only 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
Fibers
Fibers — aka “green threads” — is 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');
OPcache Performance improvements
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.
Here at QuickHostUK, we optimise our platforms, so whether you have a WordPress site or a Web Hosting site with us, you’ll benefit from PHP 8.1. We haven’t mentioned everything coming in the next release, you can read all about PHP 8.1 here.