PHP Velho Oeste 2024

不向下兼容的变更

防止 number_format() 返回负零

之前版本中,number_format() 有可能会返回 -0。虽然这是符合 IEEE 754 规范的,但是这样会导致可读性不好,新版本中会将这样的负数去掉。

<?php

var_dump
(number_format(-0.01)); // 新版本输出 string(1) "0" 旧版本输出 string(2) "-0"

转换对象和数组中的数字键

将数组转换为对象,或将对象转换为数组时,数字键现在得到了更好的处理(无论是通过显式转换还是通过 settype() 函数)。

这意味着现在可以访问数组中的整数(或者说是字符串整数)键,这些键会映射到对象中:

<?php

// array to object
$arr = [0 => 1];
$obj = (object)$arr;
var_dump(
$obj,
$obj->{'0'}, // 新写法
$obj->{0} // 新写法
);

以上示例会输出:

object(stdClass)#1 (1) {
  ["0"]=>    // 现在是字符串 key,而不是整数 key
  int(1)
}
int(1)
int(1)

从对象转换成的数组中的整数(或者说是字符串整数)键现在也可以直接访问:

<?php

// object to array
$obj = new class {
public function
__construct()
{
$this->{0} = 1;
}
};
$arr = (array)$obj;
var_dump(
$arr,
$arr[0], // 新写法
$arr['0'] // 新写法
);

以上示例会输出:

array(1) {
  [0]=>    // 现在是整数 key,而不是字符串 key
  int(1)
}
int(1)
int(1)

get_class() 函数不再接受 null 参数

之前版本中,传递 nullget_class() 函数将返回当前类名。在新版本中,此行为会抛出一个 E_WARNING 错误。如果想实现与之前版本同样的效果,请不要传递任何参数进来。

计算非可数类型(non-countable)时发出警告

对非可数类型调用 count()(或 sizeof())函数,会抛出一个 E_WARNING 错误。

<?php

var_dump
(
count(null), // NULL 不可数
count(1), // 整数不可数
count('abc'), // 字符串不可数
count(new stdClass), // 没有实现 Countable 接口的对象不可数
count([1,2]) // 数组可数
);

以上示例会输出:

Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d

Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d

Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d

Warning: count(): Parameter must be an array or an object that implements Countable in %s on line %d
int(0)
int(1)
int(1)
int(1)
int(2)

ext/hash 从资源变成对象

作为长期迁移资源的一部分,Hash 扩展已更新为使用对象而不是资源。对于 PHP 开发人员来说,该更改应该是无缝整合,除了 is_resource() 检查的地方(需要更新为 is_object())。

SSL/TLS 的默认选项的改进

下列默认选项被修改:

  • tls:// 默认为 TLSv1.0、TLSv1.1 或 TLSv1.2
  • ssl:// 成为 tls:// 的别名
  • STREAM_CRYPTO_METHOD_TLS_* 常量默认为 TLSv1.0 或 TLSv1.1 + TLSv1.2,替代之前的 TLSv1.0

gettype() 在闭包资源中的返回值

之前版本中,如果在一个闭包资源中使用 gettype() 会返回字符串 "unknown type",现在将会返回字符 "resource (closed)"

is_object()__PHP_Incomplete_Class

之前版本中,对 __PHP_Incomplete_Class 调用 is_object() 函数会返回 false,现在会返回 true

提升未定义常量的错误级别

调用未定义的常量,现在会抛出一个 E_WARNING 错误(之前版本中为 E_NOTICE)。在下一个 PHP 大版本中,将会抛出一个 Error 错误。

Windows 支持

官方支持的最低 Windows 版本为 Windows 7/Server 2008 R2。

检测 trait 的默认属性值

对 trait 默认属性值的兼容性检查将不再执行强制转换。

object 保留字的变化

object 在之前的 PHP 7.0 版本 中被声明为软保留字(soft-reserved)。现在变更为强制保留字,禁止在任何类或接口中使用该名称。

NetWare 支持

不再支持 NetWare。

SORT_STRINGarray_unique()

SORT_STRINGarray_unique(),之前复制数组并删除唯一元素(之后不会打包元素),但现在通过添加唯一元素来构建新数组。这可能会导致不同的数字索引。

bcmod() 的浮点数变更

bcmod() 函数不再将小数截断为整数。因此,其行为现在遵循 fmod(),而不是 % 运算符。例如 bcmod('4', '3.5') 现在返回 0.5 而不是 1

散列函数和非加密散列

hash_hmac()hash_hmac_file()hash_pbkdf2()hash_init()(使用 HASH_HMAC)函数将不再接受非加密散列。

json_decode() 函数变更

如果 json_decode() 的第二个选项(assoc) 为 null,则现在会使用 JSON_OBJECT_AS_ARRAY,之前始终会忽略 JSON_OBJECT_AS_ARRAY

rand()mt_rand() 输出

在 64 位机器上,自 PHP 7.1 起,由 rand()mt_rand() 为特定的种子生成的一系列随机数可能与之前版本不同(å在实现中修复了一个模偏置错误)。

sql.safe_mode ini 选项移除

sql.safe_mode ini 设置项已被移除。

date_parse()date_parse_from_format() 的变更

date_parse()date_parse_from_format() 返回数组的 zone 元素现在表示秒而不是分钟,并且其符号反转。例如 -120 现在是 7200

传入 Cookies

自 PHP 7.2.34 起,出于安全原因,传入 cookie 的 names 不再进行 url 解码。

add a note add a note

User Contributed Notes 1 note

up
0
Anonymous
4 years ago
There's a mistake on translation to French, it must be :

array_unique() avec SORT_STRING ¶
Alors que array_unique() avec SORT_STRING anciennement copiait le tableau et supprimait des éléments non uniques (sans emballer le tableau par la suite), maintenant un nouveau tableau est construit en ajoutant les éléments uniques. Cela peut entraîner des index numériques différents.
To Top