PHP 8.3 将于 2023 年 11 月 23 日发布

目录
文章目录隐藏
  1. 支持版本
  2. PHP 8.3 新特性

PHP 8.3 将于 2023 年 11 月 23 日发布

每年年底,PHP 项目都会发布新的 PHP 主要或次要版本。截至本文发布时,PHP 8.3 已经发布了 RC6 版本,按照发布计划,正式版将于 11 月 23 日发布。

支持版本

除了庆祝新的版本发布以后,也需要注意一下:PHP 8.0 的生命周期即将结束,PHP 8.0 早已在2022 年 11 月 26 日结束了积极支持,而安全支持也将在 PHP8.3 发布的三天后2023 年 11 月 26 日停止。

了解更多信息可查看Supported Versions

PHP 8.3 新特性

PHP 8.3 引入了许多新功能。然而,它的功能比 PHP 8.1 或 PHP 8.2 相对较少。

PHP 8.3 的主要新特性:

  • 类型化类常量
  • 动态类常量获取
  • #[\Override]属性
  • 只读修改
  • 添加json_validate函数
  • 添加Randomizer::getBytesFromString()方法
  • 添加Randomizer::getFloat()Randomizer::nextFloat()方法

类型化类常量

现在可以在定义常量时,增加类型。

// PHP < 8.3
interface I {
    // We may naively assume that the PHP constant is always a string
    const PHP = 'PHP 8.2';
}

class Foo implements I {
    const PHP = []; // But it may be an array...
}

// PHP 8.3
interface I {
    const string PHP = 'PHP 8.3';
}

class Foo implements I {
    const string PHP = [];
}

// Fatal error: Cannot use array as value for class constant Foo::PHP of type string

动态类常量获取

在之前的版本中获取类的常量,除了直接调用以外,想要动态获取只能通过拼接后使用constant来实现,而现在可以直接使用变量来获取常量。

这个方式在枚举类型中也可以使用。

// PHP < 8.3
class Foo {
    const PHP = 'PHP 8.2';
}

$searchableConstant = 'PHP';

var_dump(constant(Foo::class . "::{$searchableConstant}"));

// PHP 8.3
class Foo {
    const PHP = 'PHP 8.3';
}

$searchableConstant = 'PHP';

var_dump(Foo::{$searchableConstant});

添加#[\Override]属性

通过给方法添加 #[\Override] 属性,PHP 将确保在父类或实现的接口中存在同名的方法。

添加该属性可以清楚地表明重载父类方法是有意为之,并简化了重构过程,因为重载父类方法的删除会被检测到。

// PHP < 8.3 use PHPUnit\Framework\TestCase; final class MyTest extends TestCase { protected $logFile; protected function setUp(): void { $this->logFile = fopen('/tmp/logfile', 'w');
    }

    protected function taerDown(): void
    {
        fclose($this->logFile);
        unlink('/tmp/logfile');
    }
}

// The log file will never be removed, because the
// method name was mistyped (taerDown vs tearDown).

// PHP 8.3
use PHPUnit\Framework\TestCase;

final class MyTest extends TestCase
{
    protected $logFile;

    protected function setUp(): void
    {
        $this->logFile = fopen('/tmp/logfile', 'w');
    }

    #[\Override]
    protected function taerDown(): void
    {
        fclose($this->logFile);
        unlink('/tmp/logfile');
    }
}

// Fatal error: MyTest::taerDown() has #[\Override] attribute,
// but no matching parent method exists

只读修改

只读属性现在可以在魔术方法 __clone 方法中修改一次,以实现只读属性的深度克隆。

// PHP < 8.3 readonly class Foo { public \DateTime $dateTime; function __construct(\DateTime $dateTime) { $this->dateTime = $dateTime;
    }

    public function __clone()
    {
        $this->dateTime = clone $this->dateTime;
    }
}

$today = new Foo(new \DateTime());
$tomorrow = clone $today;

// Fatal error: Cannot modify readonly property Foo::$dateTime

// PHP 8.3
readonly class Foo {
    public \DateTime $dateTime;

    function __construct(\DateTime $dateTime) {
        $this->dateTime = $dateTime;
    }

    public function __clone()
    {
        $this->dateTime = clone $this->dateTime;
    }
}

$today = new Foo(new \DateTime());
$tomorrow = clone $today;

$tomorrow->dateTime->modify('+1 day');

添加json_validate函数

在之前的版本中想要验证一个字符是否是语法上有效的JSON,需要先decode然后判断错误码,而现在可以直接调用json_validate函数。

同时 json_validate() 性能比 json_decode() 要好不少,并且使用更加简单。

// PHP < 8.3
function json_validate(string $string): bool {
    json_decode($string);
    return json_last_error() === JSON_ERROR_NONE;
}

var_dump(json_validate('{ "test": { "foo": "bar" } }')); // true

// PHP 8.3
var_dump(json_validate('{ "test": { "foo": "bar" } }')); // true

一次 Lint 多个文件

PHP CLI 二进制文件的 -l 允许检查 PHP 文件以确保它没有语法错误。

以前只允许一次检查一个文件,这意味着如果想检查整个项目,则必须为每个应用程序文件调用一次它。从 PHP 8.3 开始允许传递多个文件。

// PHP < 8.3
php -l index.php

// PHP 8.3
php -l src/**/*.php

除此之外,还有一些新的类、接口和函数,以及弃用和向后兼容性中断。

具体的内容可以期待 PHP 8.3 发布后查看官方的发布公告和文档。

「点点赞赏,手留余香」

1

给作者打赏,鼓励TA抓紧创作!

微信微信 支付宝支付宝

还没有人赞赏,快来当第一个赞赏的人吧!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
码云笔记 » PHP 8.3 将于 2023 年 11 月 23 日发布

发表回复