PHP 8.1: What's New and Changed, Features and update

PHP 8.1 GA will be released on 25th November 2021. You can test your applications with PHP 8.1RC6 which is released on 11th November 2021 before the release of PHP 8.1 GA.


New Feature in PHP 8.1: Enumerations

You can find the details about Enumerations in PHP 8.1 in Enumerations RFC.

Instead of define multiple integers to represent enums, you can use the following syntax starting from PHP 8.1:

enum Suit {
  case Hearts;
  case Diamonds;
  case Clubs;
  case Spades;
}

function pick_a_card(Suit $suit) { ... }

$val = Suit::Diamonds;

pick_a_card($val);
pick_a_card(Suit::Clubs);

This is a part of the data types improvement of PHP. An Enum defines a new type, which has a fixed, limited number of possible legal values.

New Feature in PHP 8.1: Array unpacking with string keys

You can find the details about Array unpacking with string keys in PHP 8.1 in Array unpacking with string keys.

You can use the following syntax to merge two array with later string keys overwrite earlier ones:

$array = [...$array1, ...$array2];
// same as
$array = array_merge($array1, $array2);

New Feature in PHP 8.1: new in initializers

It is now possible to use "new ClassName()" expressions as parameter default values from PHP 8.1.

function test(
    $foo = new A,
    $bar = new B(1),
    $baz = new C(x: 2),
) {
}

Find more about this feature at new in initializers RFC

New Feature in PHP 8.1: read-only properties

class User {
    public function __construct(
        public readonly string $name
    ) {}
}

A readonly property can only be initialized once, and only from the scope where it has been declared. Any other assignment or modification of the property will result in an Error exception.

The immutable property allows you to avoid mistakes and improve the performance of the application.

You can find the details about read-only properties in PHP 8.1 in read-only properties RFC

New Feature in PHP 8.1: Final Class Constants

class Foo
{
    final public const X = "foo";
}

class Bar extends Foo
{
    public const X = "bar";
}
// Fatal error: Bar::X cannot override final constant Foo::X

This is also a restriction feature in PHP to void inconsistency, constants should not be modified or overrode.

You can find the details about the final modifier for class constants in PHP 8.1 in final modifier for class constants

New Feature in PHP 8.1: array_is_list()

PHP array is a flexible data map, including any key-value pairs. array_is_list() is checking if the values are having ordered keys: 1..count($array)-1.

You can find more at array_is_list RFC

New Feature in PHP 8.1: fsync & fdatasync function

When you write data into a file, the operating system may only modify the data in buffer but not the physical storage. To make sure the data are persistent on your disk, you can use fsync to flush the changes onto the disk. So when the machine lost power, the data won't be lost.

You can find more at fsync_function RFC

New Feature in PHP 8.1: Pure Intersection Types

This is a more complex new feature in PHP 8.1.

'An “intersection type” requires a value to satisfy multiple type constraints instead of a single one.' - from the RFC.

You can define the following intersection type A and B to make sure multiple data type constraints are in place.

function test(): A&B {}

You can find more at (Pure Intersection Types RFC)[https://wiki.php.net/rfc/pure-intersection-types]

New Feature in PHP 8.1: FPM metrics for Prometheus

Openmetrics status format is added to PHP-FPM since version PHP8.1. Open Swoole will also support Openmetrics status metrics in the future versions: https://github.com/openswoole/swoole-src/issues/41.

New Feature in PHP 8.1: named arguments after an argument unpack

The following syntax is allowed in PHP 8.1

foo(...$args, named: $arg)

New Feature in PHP 8.1: First-class callable syntax

Closures for callables can now be created using the syntax myFunc(...), which is the same as Closure::fromCallable('myFunc').

$fn = Closure::fromCallable('strlen');
$fn = strlen(...);
// and
$fn = Closure::fromCallable([Foo::class, 'method']);
$fn = Foo::method(...);

You can find more at First-class callable syntax

New Feature in PHP 8.1: Never return type hinting

function redirectToLoginPage(): noreturn {
    redirect('/login');
}

You can find more at Never return type RFC

New Feature in PHP 8.1: octal integer syntax "0o"/"0O"

0o16 === 14;
0O123 === 83;

You can find more at Octal integer syntax RFC

New Feature in PHP 8.1: PHP fibers

In PHP 8.1, there is native support for PHP fibers, Open Swoole will integrate with native fibers API to support the PHP tooling ecosystem such as Xdebug.

You can find more at Fibers RFC

How to upgrade to PHP 8.1

You can find a full list of changes of PHP 8.1 and upgrading guide at PHP 8.1 UPGRADE NOTES

Notice: ext-swoole is supported until v4.7.1, use ext-openswoole >= v4.7.1. Latest version: pecl install openswoole-4.8.1