PHP 8 严格类型强制应用于本机函数?
我的代码适用于 PHP 7:
round(microtime(),3);
但适用于 PHP 8:
致命错误:未捕获类型错误:round():参数 #1 ($num) 必须是 int|float 类型,[...][...]:4 中给出的字符串堆栈跟踪:#0 [...]...: round('0.21066100 1646...', 3) #1 {main} 投入 [...][...]第 4 行
如果我强制转换:
round((float)microtime(),3);
它可以工作,但 PHP 的所有强制都是自动转换且非严格类型!如果我的数十亿行代码中的所有本机函数都必须编辑以在各处强制转换,我该如何使用 PHP 8?
有没有办法保留内部函数的自动类型转换?
我在代码中以 microtime() 为例,但是:
round("200 42");
由于同样的问题,所以解决方案不仅仅是 microtime(true)
,它不是这里的主题。
问题是,如果在我的代码中我使用本机函数,则存在潜在的错误,而我无法知道它(错误仅在脚本运行时发生),所以如果我让 PHP 8,可能是我的某些页面网站不起作用,我不可能知道它!这是一个大问题!
https://www.php.net/manual/en/migration80。其他更改.php
数学函数abs()、ceil()、floor()和round()现在可以正常使用了 注意 strict_types 指令。此前,他们胁迫了第一 即使在严格类型模式下也是如此。
所以这个函数必须专注于更新代码而不是所有本地函数!
My code works on PHP 7:
round(microtime(),3);
but in PHP 8:
Fatal error: Uncaught TypeError: round(): Argument #1 ($num) must be
of type int|float, string given in [...][...]:4 Stack trace: #0
[...]...: round('0.21066100 1646...', 3) #1 {main} thrown in
[...][...] on line 4
If I force cast:
round((float)microtime(),3);
It works but all the force of PHP is autocast and non strict type! How can I use PHP 8 if all native functions in all my billions of lines codes must be edited to force cast everywhere?
Is there a way to preserve automatic type conversion on internal functions?
I take microtime() like an example in my code but:
round("200 42");
As the same problem, so the solution is not just microtime(true)
, it's not the topic here.
The problem is, if in my code I use a native function, there are potential bugs and I can't know it (error occur only when the script is running), so if I let PHP 8, may be, certain pages of my site don't work and I have no possibility to know it! It's A BIG Problem!
https://www.php.net/manual/en/migration80.other-changes.php
The math functions abs(), ceil(), floor() and round() now properly
heed the strict_types directive. Previously, they coerced the first
argument even in strict type mode.
So this function must be focused to update code and not all native functions!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要强制强制转换以匹配所有地方的本机函数签名,除非声明了
strict_types
。您确实需要确保输入到这些函数中的变量可以明确地转换为预期的数据类型。我们有一个关于数学函数的例子,无论数据类型如何,您确实希望有一个有效数字作为参数:这里字符串
“200”
有效,因为这是一个明确的数字。但是,"200 12"
不会,因为它不明确。假设您投射它:您的
"200 12"
变为(int) 200
。这是你所期望的吗?再说一次:这些是在早期版本的 PHP 中会传递的异常计算类型,幸运的是我们现在得到了
TypeError
来代替。变量只能无损进行类型转换,“鲁莽”的类型转换和损坏的数据是无数逃避错误的根源。另请参阅:PHP 数字字符串手册 ..特别是关于什么可以解释为数字以及什么不能解释的部分。基本上,如果您的变量通过
is_numeric()
,这对于数学运算来说很好——如果不是,你应该得到一条错误消息!以下是手册中的示例列表:总之,您确实希望在模糊类型转换曾经有效的地方更新代码...从长远来看,您花在更新代码上的时间会比以前花的时间少。追查因“自动转换”时损坏的变量值而导致的异常。
You don't need to force cast to match native function signatures everywhere, unless
strict_types
is declared. You do need to ensure that the variables you feed into those functions can be unambiguously typecast into the expected datatype. We have a case in point with the math functions, where you really want to have a valid number as an argument, regardless of datatypes:Here the string
"200"
works, because it's an unambiguous number. However,"200 12"
will not, since it's ambiguous. Suppose you cast it:Your
"200 12"
becomes(int) 200
. Is that what you expected? Again:These are the sorts of anomalous evaluations that would pass in earlier versions of PHP, and it's a blessing we now get a
TypeError
instead. Variables should only ever be typecast losslessly, "reckless" typecasting and corrupted data has been the source of countless evasive bugs.Also see: PHP Manual on Numeric Strings ... specifically the sections on what can be interpreted as a number and what can't. Basically, if your variable passes
is_numeric()
, it's fine for math operations -- and if not, you deserve an error message! Here's the list of samples from the manual:In conclusion, you really do want to update your code at places where ambiguous typecasting used to be valid... In the long run, you'll spend less time updating your code than you would in chasing down anomalies resulting from variable values that were corrupted when "autocasting".
in_array 也有同样的问题:
same problem with in_array: