京东6.18大促主会场领京享红包更优惠

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

2024-11-3 18:56| 发布者: c2688| 查看: 87| 评论: 0

摘要: 目次支持版本新特性范例化类常量动态类常量获取添加#[\Override]属性只读修改添加json_validate函数一次 Lint 多个文件支持版本 除了庆祝新的版本发布以后,也必要留意一下:PHP 8.0 的生命周期即将结束,PHP 8.0 早
目次

支持版本

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

相识更多信息可查看Supported Versions

新特性

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

PHP 8.3 的主要新特性:

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

范例化类常量

如今可以在定义常量时,增加范例。

[code]// 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[/code]

动态类常量获取

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

这个方式在罗列范例中也可以使用。

[code]// 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});[/code]

添加#[\Override]属性

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

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

[code]// 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[/code]

只读修改

只读属性如今可以在把戏方法 [code]__clone[/code] 方法中修改一次,以实现只读属性的深度克隆。

[code]// 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');[/code]

添加json_validate函数

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

同时 [code]json_validate()[/code] 性能比 [code]json_decode()[/code] 要好不少,而且使用更加简单。

[code]// 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[/code]

一次 Lint 多个文件

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

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

[code]// PHP < 8.3 php -l index.php // PHP 8.3 php -l src/**/*.php[/code]

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

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

以上就是PHP8.3发布内容新特性及支持版本探究的具体内容,更多关于PHP8.3版本特性的资料请关注脚本之家别的干系文章!


来源:https://www.jb51.net/program/312688n0a.htm
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
关闭

站长推荐上一条 /6 下一条

QQ|手机版|小黑屋|梦想之都-俊月星空 ( 粤ICP备18056059号 )|网站地图

GMT+8, 2025-7-1 21:48 , Processed in 0.030502 second(s), 18 queries .

Powered by Mxzdjyxk! X3.5

© 2001-2025 Discuz! Team.

返回顶部