翼度科技»论坛 编程开发 PHP 查看内容

PHP8.3更新内容新特性及支持版本探究

4

主题

4

帖子

12

积分

新手上路

Rank: 1

积分
12
支持版本

除了庆祝新的版本发布以后,也需要注意一下:PHP 8.0 的生命周期即将结束,PHP 8.0 早已在
  1. 2022 年 11 月 26 日
复制代码
结束了积极支持,而安全支持也将在 PHP8.3 发布的三天后
  1. 2023 年 11 月 26 日
复制代码
停止。
了解更多信息可查看Supported Versions

新特性

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

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

类型化类常量

现在可以在定义常量时,增加类型。
  1. // PHP < 8.3
  2. interface I {
  3.     // We may naively assume that the PHP constant is always a string
  4.     const PHP = 'PHP 8.2';
  5. }

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

  9. // PHP 8.3
  10. interface I {
  11.     const string PHP = 'PHP 8.3';
  12. }

  13. class Foo implements I {
  14.     const string PHP = [];
  15. }

  16. // Fatal error: Cannot use array as value for class constant Foo::PHP of type string
复制代码
动态类常量获取

在之前的版本中获取类的常量,除了直接调用以外,想要动态获取只能通过拼接后使用
  1. constant
复制代码
来实现,而现在可以直接使用变量来获取常量。
这个方式在枚举类型中也可以使用。
  1. // PHP < 8.3
  2. class Foo {
  3.     const PHP = 'PHP 8.2';
  4. }
  5. $searchableConstant = 'PHP';
  6. var_dump(constant(Foo::class . "::{$searchableConstant}"));
  7. // PHP 8.3
  8. class Foo {
  9.     const PHP = 'PHP 8.3';
  10. }
  11. $searchableConstant = 'PHP';
  12. var_dump(Foo::{$searchableConstant});
复制代码
添加#[\Override]属性

通过给方法添加
  1. #[\Override]
复制代码
属性,PHP 将确保在父类或实现的接口中存在同名的方法。
添加该属性可以清楚地表明重载父类方法是有意为之,并简化了重构过程,因为重载父类方法的删除会被检测到。
  1. // PHP < 8.3
  2. use PHPUnit\Framework\TestCase;
  3. final class MyTest extends TestCase
  4. {
  5.     protected $logFile;
  6.     protected function setUp(): void
  7.     {
  8.         $this->logFile = fopen('/tmp/logfile', 'w');
  9.     }
  10.     protected function taerDown(): void
  11.     {
  12.         fclose($this->logFile);
  13.         unlink('/tmp/logfile');
  14.     }
  15. }
  16. // The log file will never be removed, because the
  17. // method name was mistyped (taerDown vs tearDown).
  18. // PHP 8.3
  19. use PHPUnit\Framework\TestCase;
  20. final class MyTest extends TestCase
  21. {
  22.     protected $logFile;
  23.     protected function setUp(): void
  24.     {
  25.         $this->logFile = fopen('/tmp/logfile', 'w');
  26.     }
  27.     #[\Override]
  28.     protected function taerDown(): void
  29.     {
  30.         fclose($this->logFile);
  31.         unlink('/tmp/logfile');
  32.     }
  33. }
  34. // Fatal error: MyTest::taerDown() has #[\Override] attribute,
  35. // but no matching parent method exists
复制代码
只读修改

只读属性现在可以在魔术方法
  1. __clone
复制代码
方法中修改一次,以实现只读属性的深度克隆。
  1. // PHP < 8.3
  2. readonly class Foo {
  3.     public \DateTime $dateTime;
  4.     function __construct(\DateTime $dateTime) {
  5.         $this->dateTime = $dateTime;
  6.     }
  7.     public function __clone()
  8.     {
  9.         $this->dateTime = clone $this->dateTime;
  10.     }
  11. }
  12. $today = new Foo(new \DateTime());
  13. $tomorrow = clone $today;
  14. // Fatal error: Cannot modify readonly property Foo::$dateTime
  15. // PHP 8.3
  16. readonly class Foo {
  17.     public \DateTime $dateTime;
  18.     function __construct(\DateTime $dateTime) {
  19.         $this->dateTime = $dateTime;
  20.     }
  21.     public function __clone()
  22.     {
  23.         $this->dateTime = clone $this->dateTime;
  24.     }
  25. }
  26. $today = new Foo(new \DateTime());
  27. $tomorrow = clone $today;
  28. $tomorrow->dateTime->modify('+1 day');
复制代码
添加json_validate函数

在之前的版本中想要验证一个字符是否是语法上有效的
  1. JSON
复制代码
,需要先
  1. decode
复制代码
然后判断错误码,而现在可以直接调用
  1. json_validate
复制代码
函数。
同时
  1. json_validate()
复制代码
性能比
  1. json_decode()
复制代码
要好不少,并且使用更加简单。
  1. // PHP < 8.3
  2. function json_validate(string $string): bool {
  3.     json_decode($string);
  4.     return json_last_error() === JSON_ERROR_NONE;
  5. }
  6. var_dump(json_validate('{ "test": { "foo": "bar" } }')); // true
  7. // PHP 8.3
  8. var_dump(json_validate('{ "test": { "foo": "bar" } }')); // true
复制代码
一次 Lint 多个文件

PHP CLI 二进制文件的
  1. -l
复制代码
允许检查 PHP 文件以确保它没有语法错误。
以前只允许一次检查一个文件,这意味着如果想检查整个项目,则必须为每个应用程序文件调用一次它。从 PHP 8.3 开始允许传递多个文件。
  1. // PHP < 8.3
  2. php -l index.php
  3. // PHP 8.3
  4. php -l src/**/*.php
复制代码
除此之外,还有一些新的类、接口和函数,以及弃用和向后兼容性中断。
具体的内容可以期待 PHP 8.3 发布后查看官方的发布公告和文档。
以上就是PHP8.3发布内容新特性及支持版本探究的详细内容,更多关于PHP8.3版本特性的资料请关注脚本之家其它相关文章!

来源:https://www.jb51.net/program/312688n0a.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具