2023 年底,php 8.3.0 版本发布 (🐕 也为了冲业绩 | php 技术论坛-380玩彩网官网入口
php 8.3
- title: 2023 年底,php 8.3.0 版本发布
- tag:
php
- author: tacks
- create-date: 2023-11-25
- update-date: 2023-11-25
ref
- rfc
hello php 8.3.0
在2023年11月23日, 恭喜 php 8.3.0 新的大版本重磅发布 !
php8.3
,新增 json有效性检查函数、类常量显式类型、只读属性深拷贝,当然还有一些性能优化、错误修复,常规清理等。
它对只读类、新函数、最近添加的类的补充randomizer、堆栈溢出检测等进行了改进。
捋一下 php 更新节奏
从 php8.0.0 从 2020 发布开始,基本上每年更新一个版本,直到现在的 8.3 ,希望保持这个节奏啊,如果可以预计 2025 能看到 php9 ?
- 📌 26 nov 2020 🐘
php8.0.0
- 📌 23 nov 2021 💰
the php foundation
- 📌 25 nov 2021 🐘
php8.1.0
- 📌 08 dec 2022 🐘
php8.2.0
- 📌 23 nov 2023 🐘
php8.3.0
从时间是来看,今年比以往来得更早一些,提前完成 kpi,加油 phper 💞
个人对本次php8.3
更新的一些小理解
- 💥💥💥💥💥 日常使用方法最相关
- ⭐⭐⭐⭐ 类型声明之路逐步完善
- ☔☔☔ 特定情况下380玩彩网官网入口的解决方案
- 💧💧 不痛不痒的更新
- ⚡ 比较意外但貌似帮助也不大
- ⛔ 打破了你之前的认识
新的功能
0x1. json_validate() function (json校验函数) 💥💥💥💥💥
过去&现在
- before
验证字符串是否为有效 json 的唯一方法是对其进行 json_decode()
解码并检测是否抛出任何错误;
- now
千呼万唤始出来, json_validate()
函数来了,它只判断输入是否是有效的 json,相比 json_decode()
,使用内存更少;
用法
json_validate(string $json, int $depth = 512, int $flags = 0): bool
/**
* json 验证 (php83之前)
*
* @param string $string
* @return boolean|string
*/
function jsonvalidate(string $string): bool|string {
$data = json_decode($string);
if($data === null || json_last_error() !== json_error_none) {
return json_last_error_msg();
}
return true;
}
// php8.3
json_validate('{ "test": { "foo": "bar" } }'); // true
json_validate('{ "test": { "foo": "bar" , "test"} }'); // false
影响范围
说真的,这并不会影响我们平时的开发,因为,99%的场景下我们本身就是需要用到 json 解码后的数据来做操作,而不只是单纯的验证 json 字符串。
so,只有一些小部分场景可能需要,如你只需要判断用户的 json 内容然后录入系统中,通常如 日志类json 等。
0x2. typed class constants (类常量显式类型) ⭐⭐⭐⭐
过去&现在
- before
从 php8,就逐步布局类型定义,虽然背后还是弱类型,但提供了类型声明,就能让编译器/解析器,提前告诉你代码可能的问题,但类常量还没有支持,这不,一步步完善
- now
php8.3 ,增加了类常量的类型定义,如果你给了不符合类型的值,将会抛出 fatal error
致命错误,当然这不是必须的,你依然可以不参用类型声明
用法
class beforefoo
{
// php83 之前,可以直接这么定义
const bar = 'bar';
}
class foo
{
// php83 时,可以给常量也加上类型声明
const string bar = 'bar';
}
class errfoo
{
// php83 时,你可以给常量加类型声明,但如果错误,将会抛出异常
// fatal error: cannot use string as value for class constant errfoo::bar of type int
const int bar = 'bar';
}
0x3. dynamic class constant fetch (动态类常量获取) 💧💧
过去&现在
- before
php 貌似很喜欢玩动态类型获取。比如 $$var
可变变量,放眼其他编程语言貌似都没这么玩的 ,顺便看看 到底有哪些动态获取成员名称的手段
// 可变变量
$$foo;
// 静态属性
foo::${$bar};
// 静态属性
$obj::$bar;
// 成员属性
$obj->$foo;
// 静态方法
foo::{$bar}();
// 静态方法
$obj::bar();
// 方法
$foo->{$bar}();
- now
看到 类常量动态获取的方式 还没引入,php83 那就引入吧
// 类常量
foo::{$name};
用法
class foo
{
const bar = 'bar';
public $name = '1';
}
$name = 'bar';
// php8.3 之前
echo constant(foo::class . '::' . $name) , php_eol;
// php8.3 现在
echo foo::{$name} , php_eol;
// php8.3 现在 切记给变量带上 {} ,它标识先解析 name 变量,再获取类常量
// 下面是错误用法:fatal error: uncaught error: access to undeclared static property foo::$name
// echo foo::$name;
更多
class myclass {
const var1 = 'class constants';
public static $var2 = 'class static property';
public $var3 = 'class property';
public static function func1() { return 'class static function '. __function__; }
public function func2() { return 'class function '. __function__; }
}
echo '【类常量-类-直接获取】 | myclass::var1 | ', myclass::var1 , php_eol;
echo '【类静态属性-类-直接获取】 | myclass::$var2 | ', myclass::$var2 , php_eol;
echo '【成员属性-对象-直接获取】 | (new myclass())->var3 | ', (new myclass())->var3 , php_eol;
echo "================================================================================================", php_eol;
$bar = 'var1';
echo '【类常量-类-动态获取】 | bar = var1 | myclass::{$bar}; | ', myclass::{$bar} , php_eol;
$bar = 'var2';
echo '【类静态属性-类-动态获取】 | bar = var2 | myclass::${$bar}; | ', myclass::${$bar} , php_eol;
$bar = 'var2';
echo '【类静态属性-对象-动态获取】 | bar = var2 | (new myclass)::${$bar}; | ', (new myclass)::${$bar} , php_eol;
$bar = 'var3';
echo '【成员属性-对象-动态获取】 | bar = var3 | (new myclass)->${$bar}; | ', (new myclass)->{$bar} , php_eol;
$bar = 'func1';
echo '【静态方法-类-动态获取】 | bar =func1 | myclass::{$bar}(); | ', myclass::{$bar}() , php_eol;
echo '【静态方法-对象-动态获取】 | bar =func1 | (new myclass)::{$bar}(); | ', (new myclass)::{$bar}() , php_eol;
$bar = 'func2';
echo '【成员方法-对象-动态获取】 | bar =func2 | (new myclass)->{$bar}(); | ', (new myclass)->{$bar}() , php_eol;
// 别忘记枚举值
enum status: string {
case s_ok = 'ok';
}
$bar = 's_ok';
echo '【枚举值-枚举-动态获取】 | bar =s_ok | status::{$bar}->value; | ', status::{$bar}->value , php_eol;
0x4. deep clone readonly properties (只读属性深拷贝) ☔☔☔
用法
readonly class people
{
public function __construct(
public string $name,
public string $age,
public datetime $createdat,
) {}
public function __clone()
{
$this->createdat = new datetime();
}
}
$tacks = new people(
name: 'tacks',
age: 18,
createdat: new datetime(),
);
var_dump($tacks);
$nextme = clone $tacks;
// php8.3 之前 fatal error: uncaught error: cannot modify readonly property people::$createdat
// php8.3 可用 在克隆过程中可以重新初始化只读属性
var_dump($nextme);
补充:匿名只读类也在 php8.3支持了
// php8.3 之前 parse error: syntax error, unexpected token "readonly"
// php8.3 可用
$other = new readonly class {
public function __construct(
public string $foo = 'bar',
) {}
};
var_dump($other->foo);
0x5. marking overriden methods with new attribute (标记重写方法 #[\override]
) ⭐⭐⭐⭐
过去&现在
- before
使用 #[override]
注解标记方法,表示您知道该方法正在重写父方法,它唯一要做的就是表现出意图,防止父类如果无意间被修改了方法名,将会导致子类的不可预见性错误。
- now
但是现在,php8.3 有了 #[override]
注解,让 php 知道了子类的这个方法是覆盖父类的,如果没有则会抛出错误,当然这也更加友好,因为如果你的 ide 如果支持,或者有 静态分析器可以提前检查对应的错误。
fatal error: myclass::foo() has #[\override] attribute, but no matching parent method exists
使用
abstract class father
{
public function foo(): int
{
return 1;
}
}
final class myclass extends father
{
#[override]
public function foo(): int
{
return 2; // the overridden method
}
}
0x6. new randomizer::getfloat() and randomizer::nextfloat() methods (randomizer类新增两个浮点数方法) 💥💥💥💥💥
由于浮点数的精度有限和隐式舍入,生成位于特定区间内的无偏浮点数并非易事,并且常用的用户态380玩彩网官网入口的解决方案可能会生成有偏差的结果或超出请求范围的数字。
所以说官方出手了,还是比自己封装的要高效好用,之后随机浮点数可以来参考这个了。
# 获取随机浮点数,在 min,max ,第三个参数则是决定是否包含边界
public random\randomizer::getfloat(float $min, float $max, random\intervalboundary $boundary = random\intervalboundary::closedopen): float
# 获取 (0,1) 之间的浮点数
public random\randomizer::nextfloat(): float
使用
// 新增 getfloat() nextfloat()
$randomizer = new \random\randomizer();
// getfloat($min, $max) 返回之间的浮点数
// closed表示包含该值,表示open排除该值
$temperature = $randomizer->getfloat(
-89.2,
56.7,
\random\intervalboundary::closedclosed,
);
var_dump($temperature);
// nextfloat() 它将为您提供 0 到 1 之间的随机浮点数,其中 1 被排除
var_dump($randomizer->nextfloat());
$chancefortrue = 0.8;
$myboolean = $randomizer->nextfloat() < $chancefortrue;
var_dump($myboolean);
0x7. command line linter supports multiple files (支持 -l 检查多个文件) ⚡
过去&现在
说实话,用 php 也挺久的了,竟然没发现,或者说没用过 -l
选项。要不是这次看了 ,把这个功能明显的列出来,我还真不知道,哈哈哈哈哈。
- before
php8.3 之前, php -l test.php
只支持单个文件,及时你写多个文件,也是以第一个文件为准
- now
php8.3 时, php -l test1.php test2.php
支持多个文件了,会按照顺序依次检查
使用
$ php -l test1.php test2.php
parse error: syntax error, unexpected end of file in test1.php on line 21
errors parsing test1.php
parse error: syntax error, unexpected token "{", expecting identifier in test2.php on line 5
errors parsing test2.php
0x8. negative indices in arrays (数组中的负数索引) ⛔
过去&现在
- before
php8.3 之前, 第一个索引是负数,第二个默认还是 0 开始
- now
php8.3 时,第一个索引是负数,第二个默认则是加一之后的索引,而不一定是 0
使用
$array = [];
$array[-9] = 'a';
$array[] = 'b';
var_export($array);
/*
===> php8.3 之前
array (
-9 => 'a',
0 => 'b',
)
===> php8.3 时
array (
-9 => 'a',
-8 => 'b',
)
*/
0x9. new posix’s function (posix扩展相关函数的新增) 💧💧
0x10. other
更多更新细节,可以看
堆栈溢出检测
php8.3 添加了两个新的 ini 指令
zend.max_allowed_stack_size
- 0 意味着 php 将自动确定一个值
- -1 表明没有限制或特定的字节数
- 现有
fiber.stack_size
指令用作允许的最大堆栈大小
zend.reserved_stack_size
用于确定“缓冲区”, 以便 php 仍然能够抛出错误而不是实际耗尽内存- 这里的值应该是多个字节,但 php 将为您确定一个合理的默认值,无需填写
好处: error 当使用超过 和 zend.max_allowed_stack_size
之间的差异时,接近溢出调用堆栈的程序现在可能会抛出 zend.reserved_stack_size
。此功能的好处是堆栈溢出引起的分段错误将不再导致段错误,从而使调试变得更加容易。
新 mb_str_pad() 功能
在 php 中,各种字符串函数有两种变体:一种用于字节字符串,另一种用于多字节字符串。然而,多字节字符串函数中值得注意的一个缺陷 mbstring
是 str_pad()
该 str_pad()
函数缺乏多字节字符支持,因此在使用使用多字节编码(如 utf-8)的语言时会出现问题。该 rfc 建议在 php 中添加这样一个函数,我们将其称为 mb_str_pad()
非rfc提交
并非 php 中的每个更改都会通过 rfc 流程。事实上,大多数更改包括维护和错误修复,并且不需要 rfc。所有这些更改都列在升级文档中。
- 使用ffi时,返回类型为
void
现在返回null
而不是 c 函数ffi\cdata:void
posix_getrlimit()
现在采用可选$res参数来允许获取单个资源限制gc_status()
有四个新字段 running、protected、full 、buffer_sizeclass_alias()
现在支持创建内部类的别名array_pad()
现在仅受数组可以拥有的最大元素数的限制。之前,一次最多只能添加1048576
个元素- 在 posix 系统上执行
proc_get_status()
多次将始终返回正确的值 opcache.consistency_checksini
指令已删除- 改进
array_sum()
和array_product()
本作品采用《cc 协议》,转载必须注明作者和本文链接
tacks
无关痛痒的更新
还有一条,在windows上,最低支持版本是 windows 8 或 windows server 2012。
minimum supported windows version has been bumped to windows 8 or windows server 2012.
介绍的非常详细,感谢!