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.
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.
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);
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
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
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
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
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
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]
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.
The following syntax is allowed in PHP 8.1
foo(...$args, named: $arg)
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
function redirectToLoginPage(): noreturn {
redirect('/login');
}
You can find more at Never return type RFC
0o16 === 14;
0O123 === 83;
You can find more at Octal integer syntax RFC
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
You can find a full list of changes of PHP 8.1 and upgrading guide at PHP 8.1 UPGRADE NOTES
Join 4,000+ others and never miss out on new tips, tutorials, and more.