PHP 8.1 is here

The PHP team has released PHP 8.1. Let’s see a bit of some main features they have added.

https://twitter.com/official_php/status/1463943033234276360

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



アプリ関連ニュース

お問い合わせはこちら

お問い合わせ・ご相談はお電話、またはお問い合わせフォームよりお受け付けいたしております。

tel. 06-6454-8833(平日 10:00~17:00)

お問い合わせフォーム