What’s new in PHP 8.1

What’s new in PHP 8.1

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.

78% of the web powered by PHP

78% of the web powered by PHP

Today, PHP is used by almost 80% of all the websites whose server-side programming language we know. Popular websites such as Slack, Etsy, Wikipedia, WordPress, Mailchimp, Canva, Indeed, Investing.com, and others are powered by PHP.

However, in the coming months, many websites that fail to upgrade to the latest version of PHP 8 will be left running unsupported versions.

PHP 7 was released back in 2015, and 68% of PHP-based websites still use PHP 7. Meanwhile, PHP 8 was released in November 2020, yet only ~ 1% of PHP-based websites currently use it!

This slow uptake of PHP 8 will become a much higher priority upgrade for organizations very soon. In fact, PHP 7 loses active support next month and loses security support in one year!

Extended support for PHP 7.4 will end on November 28, 2022. After this date, applications will continue to run; however, those applications will be out of support and at risk of security vulnerabilities that remain unpatched.

In addition, after November 28, 2022, Microsoft will no longer support PHP, and mainly Unix-based distros such as Linux will be the OS supported by future versions of PHP and continued feature, quality, and security updates.

Symfony 4.4.33 released

Symfony 4.4.33 released

Symfony 4.4.33 has just been released. Here is a list of the most important changes:

  • bug #43798 [Dotenv] Duplicate $_SERVER values in $_ENV if they don’t exist (@fancyweb)
  • bug #43799 [PhpUnitBridge] fix symlink to bridge in docker by making its path relative (@nicolas-grekas)
  • bug #43781 [Messenger] Fix TraceableMessageBus implementation so it can compute caller even when used within a callback (@Ocramius)
  • bug #43655 [VarDumper] Fix dumping twig templates found in exceptions (@event15)
  • bug #43484 [Messenger] Fix Redis Transport when username is empty (@villfa)
  • bug #43568 [Messenger] fix: TypeError in PhpSerializer::encode() (@dsech)
  • bug #43591 [Config] Fix files sorting in GlobResource (@lyrixx)
  • bug #43569 [HttpClient] fix collecting debug info on destruction of CurlResponse (@nicolas-grekas)
  • bug #43545 [DependencyInjection] fix “url” env var processor (@nicolas-grekas)
  • bug #43413 [VarDumper] Fix error with uninitialized XMLReader (@villfa)
  • bug #43388 [Validator] Fixes URL validation for single-char subdomains (@DfKimera)
  • bug #43333 [HttpClient] fix missing kernel.reset tag on TraceableHttpClient services (@nicolas-grekas)
  • bug #43302 [Cache] Commit items implicitly only when deferred keys are requested (@Sergey Belyshkin)
  • bug #43330 [Cache][Lock] fix SQLSRV throws for method_exists() (@GDmac)
  • bug #43270 [VarDumper] Fix handling of “new” in initializers on PHP 8.1 (@nicolas-grekas)
  • bug #43277 [DependencyInjection] fix support for “new” in initializers on PHP 8.1 (@nicolas-grekas)
  • bug #43243 [HttpClient] accept headers when CURLE_RECV_ERROR is received before the content (@nicolas-grekas)
  • bug #43205 [Serializer] Fix denormalizing XML array with empty body (4.4) (@alexandre-daubois)

Want to upgrade to this new release? Because Symfony protects backwards-compatibility very closely, this should be quite easy. Use SymfonyInsight upgrade reports to detect the code you will need to change in your project and read our upgrade documentation to learn more.