IT资讯 PHP 8.3 GA

dominic · 2023-11-23 14:00:08 · 热度: 3

PHP 8.3 已正式发布。主要变化包括:

  • 类常量显式类型 (Typed class constants)
  • 只读属性深拷贝
  • 新增#[\Override]属性
  • 新增json_validate()函数
  • 添加Randomizer::getBytesFromString()方法
  • 添加Randomizer::getFloat()Randomizer::nextFloat()方法
  • 以及更好的性能、更好的语法、改进类型安全

PHP 8.3 GA

下面介绍部分语法的变化。

  • 类型化类常量

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 {
    // But implementing classes may define it as an array.
    const PHP = [];
}

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
  • 动态获取类常量

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});
  • 只读属性深拷贝

readonly属性现在可以在魔术方法__clone中被修改一次,以此实现只读属性的深拷贝。

PHP < 8.3

class PHP {
    public string $version = '8.2';
}

readonly class Foo {
    public function __construct(
        public PHP $php
    ) {}

    public function __clone(): void {
        $this->php = clone $this->php;
    }
}

$instance = new Foo(new PHP());
$cloned = clone $instance;

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

PHP 8.3

class PHP {
    public string $version = '8.2';
}

readonly class Foo {
    public function __construct(
        public PHP $php
    ) {}

    public function __clone(): void {
        $this->php = clone $this->php;
    }
}

$instance = new Foo(new PHP());
$cloned = clone $instance;

$cloned->php->version = '8.3';
  • 新增#[\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
  • 新增json_validate()函数

json_validate()可以检查一个字符串是否为语法正确的 JSON,比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

此外,PHP 8.0 的生命周期即将结束。早在 2022 年 11 月 26 日,PHP 8.0 结束了积极支持,而安全支持也将在 PHP 8.3 正式发布三天后——2023 年 11 月 26 日停止。

为您推荐与 php 相关的帖子:

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册