CakeFest 2024: The Official CakePHP Conference

PHP 7.2.x 中废弃的功能

不带引号的字符串

不带引号的字符串是不存在的全局常量,转化成他们自身的字符串。 在以前,该行为会产生 E_NOTICE,但现在会产生 E_WARNING。在下一个 PHP 主版本中,将抛出 Error 异常。

<?php

var_dump
(NONEXISTENT);

/* Output:
Warning: Use of undefined constant NONEXISTENT - assumed 'NONEXISTENT' (this will throw an Error in a future version of PHP) in %s on line %d
string(11) "NONEXISTENT"
*/

png2wbmp()jpeg2wbmp()

GD 扩展内的 png2wbmp()jpeg2wbmp() 现已被废弃,将在下一个 PHP 主版本中移除。

INTL_IDNA_VARIANT_2003 转化

Intl 扩展废弃了 INTL_IDNA_VARIANT_2003 转化,为idn_to_ascii()idn_to_utf8() 的默认选项。 PHP 7.4 会把默认值设置为 INTL_IDNA_VARIANT_UTS46, 并在下一个 PHP 主版本中完全移除 INTL_IDNA_VARIANT_2003

__autoload() 方法

__autoload() 方法已被废弃, 因为和 spl_autoload_register() 相比功能较差 (因为无法链式处理多个 autoloader), 而且也无法在两种 autoloading 样式中配合使用。

track_errors ini 设置和 $php_errormsg 变量

当开启了 track_errors ini 设置,出现非致命错误时, 会在本地作用域创建 $php_errormsg 变量。 由于提供了更好的方式: error_get_last() 来获取此类错误信息,该功能被废弃。

create_function() 函数

考虑到此函数的安全隐患问题(它是 eval() 的瘦包装器),该过时的函数现在已被废弃。 更好的选择是匿名函数

mbstring.func_overload ini 设置

由于此设置会影响环境中的字符串系列函数,带来相互操作中的问题,它现在已被废弃。

(unset) 类型强制转化

转化任意表达式为此类型,结果总是 null,所以这个多余的类型转化现在也就被废弃了。

parse_str() 不加第二个参数

使用 parse_str() 时,不加第二个参数会导致查询字符串参数导入当前符号表。 考虑到安全隐患问题,不加第二个参数使用 parse_str() 的行为已被废弃。 此函数的第二个选项为必填项,它使查询字符串转为 Array。

gmp_random() 函数

此函数基于未知的、取决于平台的 limb 尺寸产生随机数。因此,该函数已被废弃。 使用更好的方式产生随机数: GMP 扩展中的 gmp_random_bits()gmp_random_range()

each() 函数

使用此函数遍历时,比普通的 foreach 更慢, 并且给新语法的变化带来实现问题。因此它被废弃了。

assert() 一个字符串参数

assert() 字符串参数将要求它能被 eval() 执行。 考虑到可能被执行远程代码,废弃了字符串的 assert(),最好提供 bool 的表达式。

错误处理器内的 $errcontext 参数

$errcontext 参数包含了错误网站的所有本地变量。 考虑到它很少被用到,而且还会导致内部优化问题,它现在被废弃了。 代替用法:调试器应该自己取回错误站点的本地变量。

read_exif_data() 函数

read_exif_data() 别名已被废弃 使用 exif_read_data() 函数代替。

add a note

User Contributed Notes 2 notes

up
4
Anonymous
5 years ago
An empty <?php?> Codeblock in PHP 7.2.0 will put out an "Unquoted strings" warning. To prevent that, add at least one empty space into the codeblock.

Example:
<?php

<?php/*
echo "This was some useful code.\n";
*/
?>

?>
Gives: PHP Warning: Use of undefined constant php - assumed 'php' (this will throw an Error in a future version of PHP)

Fix via:
<?php

<?php /*
echo "This was some useful code.\";
*/
?>

?>

Don't know if that is an intentional behaviour or a side effect.
up
2
webmaster at thedigitalorchard dot ca
6 years ago
Instead of __autoload(), you can use spl_autoload_register() very easily, as per the documentation:

spl_autoload_register(function ($class) {
include 'classes/' . $class . '.class.php';
});

And this lets you have multiple autoloaders instead of one global one.
To Top