美文网首页php技术社区
PHP 8.1.0 正式发布了

PHP 8.1.0 正式发布了

作者: IT小小码农 | 来源:发表于2021-11-26 14:25 被阅读0次

    PHP 8.1.0 现已发布,该版本带来了许多改进和新功能。

    枚举

    使用枚举而不是一组常量并立即进行验证。

    enum Status
    {
        case draft;
        case published;
        case archived;
    
        public function color(): string
        {
            return match($this)
            {
                Status::draft => 'grey',
                Status::published => 'green',
                Status::archived => 'red',
            };
        }
    } 
    

    只读属性

    只读属性不能在初始化后更改,比如,在为它们分配值后。它们可以用于对值对象和数据传输对象建模。

    class PostData
    {
        public function __construct(
            public readonly string $title,
            public readonly string $author,
            public readonly string $body,
            public readonly DateTimeImmutable $createdAt,
            public readonly PostState $state,
    
        ) {}
    
    }
    

    First-class 可调用语法

    现在可以获得对任何函数的引用。

    function foo(int $a, int $b) { /* … */ }
    $foo = foo(...);
    $foo(a: 1, b: 2);
    

    新的初始化器

    对象现在可以用作默认参数值、静态变量和全局常量,以及属性参数,这有效地使使用嵌套属性成为可能。

    class PostStateMachine
    {
        public function __construct(
           private State $state = new Draft(),
    
        ) {
    
        }
    }
    

    纯交集类型

    当一个值需要同时满足多个类型约束时,使用交集类型。注意,目前无法将交集和联合类型混合在一起,例如 A&B|C。

    function generateSlug(HasTitle&HasId $post) {
        return strtolower($post->getTitle()) . $post->getId();
    }
    

    Never 返回类型

    使用 never 类型声明的函数或方法表示它不会返回值,并且会抛出异常或通过调用 die()exit()``、trigger_error()` 或类似的东西来结束脚本的执行。

    function dd(mixed $input): never
    {
        // dump 
        exit;
    }
    

    never不同之处void,在于void仍然允许程序继续。这似乎是一个新奇的功能,但它实际上对静态分析器来说是一个非常有用的功能。

    Final 类常量

    可以声明 final 类常量,以禁止它们在子类中被重写。

    class Foo
    {
        public const X = "foo";
    }
    
    class Bar extends Foo
    {
        public const X = "bar";
    }
    

    显式八进制数字表示法

    您可以使用0o和0O来表示八进制数。前面通过在数字前面加上前缀的表示法0仍然有效。

    16 === 0o16; // true
    016 === 0O16; // true
    

    纤程

    Fibers 是用于实现轻量级协作并发的原语。它们是一种创建可以像生成器一样暂停和恢复的代码块的方法,但可以从堆栈中的任何位置进行。Fibers 本身并没有提供并发性,仍然需要一个事件循环。但是,它们允许通过阻塞和非阻塞实现共享相同的 APIFibers 允许摆脱以前在 Promise::then() 或基于生成器的协程中看到的样板代码。库通常会围绕 Fiber 构建进一步的抽象,因此无需直接与它们交互。

    $fiber = new Fiber(function (): void {
        $valueAfterResuming = Fiber::suspend('after suspending');
    
        // … 
    });
    
    $valueAfterSuspending = $fiber->start();
    
    $fiber->resume('after resuming');
    

    对字符串键控数组的数组解包支持

    PHP 以前支持通过扩展运算符在数组内部解包,但前提是数组具有整数键。现在也可以使用字符串键解包数组。

    $array1 = ["a" => 1];
    $array2 = ["b" => 2];
    $array = ["a" => 0, ...$array1, ...$array2];
    
    var_dump($array); // ["a" => 1, "b" => 2]
    

    新array_is_list功能

    可能偶尔不得不处理这个问题:确定数组的键是否按数字顺序排列,从索引 0 开始。就像json_encode决定数组应该编码为数组还是对象一样。

    $list = ["a", "b", "c"];
    array_is_list($list); // true
    
    $notAList = [1 => "a", 2 => "b", 3 => "c"];
    array_is_list($notAList); // false
    
    $alsoNotAList = ["a" => "a", "b" => "b", "c" => "c"];
    array_is_list($alsoNotAList); // false
    

    性能优化

    PHP 8.1 在性能方面也有一些改动,包括:

    • ARM64 的 JIT 后端 (AArch64)

    • 继承缓存(避免在每个请求中重新链接类)

    • 快速类名解析(避免小写和哈希查找)

    • timelibext/date 性能改进

    • SPL 文件系统迭代器改进

    • 序列化/反序列化优化

    • 一些内部函数优化(get_declared_classes()explode()strtr()strnatcmp()dechex()

    • JIT 改进和修复

    文章来自微信公众号:PHP自学中心

    相关文章

      网友评论

        本文标题:PHP 8.1.0 正式发布了

        本文链接:https://www.haomeiwen.com/subject/ewtxxrtx.html