支持版本
除了庆祝新的版本发布以后,也需要注意一下: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版本特性的资料请关注代码网其它相关文章!
发表评论