CakeFest 2024: The Official CakePHP Conference

PHP 7.4.x 废弃的功能

PHP 核心中废弃的功能

没有显式括号的嵌套三元运算符

嵌套的三元操作中,必须明确使用显式括号来决定操作的顺序。以前,如果不使用括号,在大多数情况下,左关联性不会导致预期的行为。

<?php
1
? 2 : 3 ? 4 : 5; // 弃用
(1 ? 2 : 3) ? 4 : 5; // ok
1 ? 2 : (3 ? 4 : 5); // ok
?>

当运算对象嵌套到中间时,需要括号,因为没有歧义且不受关联顺序的影响:

1 ? 2 ? 3 : 4 : 5 // ok

大括号访问数组和字符串索引

使用大括号访问数组及字符串索引的方式已被废弃。请使用 $var[$idx] 的语法来替代 $var{$idx}

(real) 类型和 is_real() 函数

(real) 类型已被废弃,请使用 (float) 来替代。

同时被废弃的还有 is_real() 函数,请使用 is_float() 来替代。

Unbinding $this when $this is used

弃用在未捆绑 $this 的非静态 closure 中使用 $this

parent 关键词在没父类的类中使用

在没有父类的类中使用 parent 关键词已被废弃,并且在将来的 PHP 版本中将会抛出一个编译错误。目前只在运行时访问父类时才会产生错误。

allow_url_include INI 选项

配置文件中的 allow_url_include 选项被废弃。如果启用了该选项,将会产生一个弃用通知。

基础转换函数中的无效字符处理

在下面这些基础转换函数中,base_convert(), bindec(), octdec()hexdec() 如果传入了非法字符,将会抛出一个弃用通知。函数会忽略掉无效字符后正常返回结果。前导空格和尾部空格,以及类型为 0x (取决于基数) 被允许传入。

在对象中使用 array_key_exists()

在一个对象中使用 array_key_exists() 已被废弃。请使用 isset()property_exists() 来替代。

魔术引号函数

魔术引号函数 get_magic_quotes_gpc()get_magic_quotes_runtime() 已被废弃。它们将永远返回 false

hebrevc() 函数

hebrevc() 函数已被废弃。 可以用 nl2br(hebrev($str)) 来替代,更好的方法是启用 Unicode RTL 来支持。

convert_cyr_string() 函数

convert_cyr_string() 函数已被废弃。可以用 mb_convert_string()iconv()UConverter 替代。

money_format() 函数

money_format() 函数已被废弃。 可以用更国际化的 NumberFormatter 功能来替代。

ezmlm_hash() 函数

ezmlm_hash() 函数已被废弃。

restore_include_path() 函数

restore_include_path() 函数已被废弃。可以用 ini_restore('include_path') 替代。

Implode 函数的参数顺序

implode() 允许反转参数顺序的特性已被废弃,请使用 implode($glue, $parts) 来替代 implode($parts, $glue)

COM

导入类型库的大小写不敏感的常量注册已被废弃。

Filter

FILTER_SANITIZE_MAGIC_QUOTES 已被废弃,使用 FILTER_SANITIZE_ADD_SLASHES 来替代。

多字节字符串

弃用向 mb_ereg_replace() 传递非字符串 pattern。目前,非字符串 pattern 被解释为 ASCII 码点。在 PHP 8 中,pattern 将被解释为字符串。

弃用传递 encoding 作为 mb_strrpos() 的第三个参数。而是替换为第三个参数 offset 默认传 0,且 encoding 作为第四个参数。

轻量目录访问协议

ldap_control_paged_result_response()ldap_control_paged_result() 函数已被废弃。控制页面操作可以使用 ldap_search() 替代。

Reflection

调用 ReflectionType::__toString() 现在将会抛出一个弃用通知。 该方法从 PHP 7.1 开始,在 ReflectionNamedType::getName() 的文档中已经被声明废弃,但是由于技术原因,并没有抛出弃用通知。

弃用所有 Reflection 类的 export() 方法。而是替换为构造 Reflection 对象并转换为字符串:

<?php
// ReflectionClass::export(Foo::class, false) 是:
echo new ReflectionClass(Foo::class), "\n";

// $str = ReflectionClass::export(Foo::class, true) 是:
$str = (string) new ReflectionClass(Foo::class);
?>

Socket

常量 AI_IDN_ALLOW_UNASSIGNEDAI_IDN_USE_STD3_ASCII_RULESsocket_addrinfo_lookup() 中不再可用,因为该常量在 glibc 中已被废弃。

add a note

User Contributed Notes 2 notes

up
11
Ahmad Asjad
3 years ago
(\?[^php]).*(\:).*(\?).*(\:[^=])
Above regex can help others to find the nested ternary operator
up
-23
Techlemur
3 years ago
^((?!\().*)(\?[^php]).*(\:)([\s\v]+)(?!\().*[^\/](\?)([\s\v\h]*).*(\:[^=])
Regex to find deprecated nested ternaries
To Top