目录
- 支持版本
- 新特性
- 类型化类常量
- 动态类常量获取
- 添加#[\\Override]属性
- 只读修改
- 添加json_validate函数
- 一次 Lint 多个文件
支持版本
除了庆祝新的版本发布以后,也需要注意一下: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()方法
类型化类常量
现在可以在定义常量时,增加类型。
// 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 发布后查看官方的发布公告和文档。
以上就是PHP8.3发布内容新特性及支持版本探究的详细内容,更多关于PHP8.3版本特性的资料请关注悠久资源网其它相关文章!
您可能感兴趣的文章:
- ThinkPHP3.1.3版本新特性概述
- PHP5各个版本的新功能和新特性总结
- PHP5的版本和PHP7之间的区别详解
- 2020最新版 PhpStudy V8.1版本下载安装使用详解
- PHPStorm2020.1永久激活及下载更新至2020(推荐)