当前位置:首页 > PHP教程 > php环境安装 > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2024-03-16 13:15:15 浏览: 评论:0 

每年年底,PHP 项目都会发布新的 PHP 主要或次要版本,截至本文发布时,PHP 8.3 已经发布了 RC6 版本,按照发布计划,正式版将于 11 月 23 日发布,这篇文章主要为大家介绍了PHP8.3发布内容新特性及支持版本实例探究。

支持版本

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

了解更多信息可查看Supported Versions。

新特性

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

PHP 8.3 的主要新特性:

类型化类常量

动态类常量获取

#[\Override]属性

只读修改

添加json_validate函数

添加Randomizer::getBytesFromString()方法

添加Randomizer::getFloat()和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.  
  7. class Foo implements I { 
  8.     const PHP = []; // But it may be an array... 
  9. } 
  10.  
  11. // PHP 8.3 
  12. interface I { 
  13.     const string PHP = 'PHP 8.3'; 
  14. } 
  15.  
  16. class Foo implements I { 
  17.     const string PHP = []; 
  18. } 
  19.  
  20. // Fatal error: Cannot use array as value for class constant Foo::PHP of type string 

动态类常量获取

在之前的版本中获取类的常量,除了直接调用以外,想要动态获取只能通过拼接后使用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]属性

通过给方法添加 #[\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 

只读修改

只读属性现在可以在魔术方法 __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函数

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

同时 json_validate() 性能比 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 二进制文件的 -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 发布后查看官方的发布公告和文档。

Tags: PHP8.3新特性

分享到: