PHP 8.1 is here
- 2021年12月06日
- 技術情報
The PHP team has released PHP 8.1. Let’s see a bit of some main features they have added.
Improvements
There are many improvements as follow.
- Enumerations
- Readonly properties
- Pure Intersection Types
- never return type
- First-class Callable Syntax
- New array_is_list function
- and many more
We will walk through some feature more details.
Enumerations
PHP 8.1 supports Enumerations (Enums) natively, providing an API for defining and working with Enums:
enum Data
{
case Draft;
case Published;
case Archived;
}
function acceptStatus(Data $data) {...}
Read-only Properties
Read-only properties cannot be changed after they are initialized. You can be confident that your data classes are consistent. PHP 8.1 can reduce boilerplate by defining public properties the author does not intend to change.
class Sample
{
public readonly Data $data;
public function __construct(Data $data)
{
$this->data = $data;
}
}
First-class Callable Syntax
You can make make a closure from a callable by calling it and passing "...
“
function sum(int $a, int $b) {
// ...
}
$sum = sum(...);
$sum(1, 5);
$sum(5, 3);
and there are still more interesting features to look more detail. Please check the official documentation for more detail.
Yuuma
yuuma at 2021年12月06日 10:00:00