PHP 8.0 废弃的功能
PHP 核心中废弃的功能
-
如果带有默认值的参数后面跟着一个必要的参数,那么默认值就会无效。这在 PHP 8.0.0 中已被废弃,通常可以通过删除默认值,不影响现有功能:
<?php
function test($a = [], $b) {} // 之前
function test($a, $b) {} // 之后
?>这条规则的一个例外是
Type $param = null
形式的参数,其中 null 的默认值使得类型隐式为空。这种用法仍然是允许的,但仍建议使用显式可空类型。<?php
function test(A $a = null, $b) {} // 旧写法,仍可用
function test(?A $a, $b) {} // 推荐写法
?> -
参数
exclude_disabled
不能设置为false
来调用 get_defined_functions(),该参数已被废弃,不再起作用。 get_defined_functions() 绝不会再包含禁用的函数。
Enchant
-
弃用 enchant_broker_set_dict_path() 和 enchant_broker_get_dict_path() ,因为此功能在 libenchant < 1.5 和 libenchant-2 中均不可用。
-
弃用 enchant_dict_is_in_session(),使用 enchant_dict_is_added() 替代。
-
弃用 enchant_broker_free() 和 enchant_broker_free_dict(),取而代之的是取消对象。
-
弃用
ENCHANT_MYSPELL
和ENCHANT_ISPELL
常量。
LibXML
弃用 libxml_disable_entity_loader()。由于现在需要 libxml
2.9.0,默认情况下会禁用外部实体加载并且不再需要此函数来防止 XXE 攻击,除非使用(仍然易受攻击的)
LIBXML_NOENT
。在这种情况下,建议使用
libxml_set_external_entity_loader() 重构代码以抑制外部实体加载。
PGSQL / PDO PGSQL
-
现在常量
PGSQL_LIBPQ_VERSION_STR
的值与PGSQL_LIBPQ_VERSION
相同,因此废弃。 -
pgsql 扩展中的函数别名已废弃。请参阅以下列表了解应该使用那些函数:
- pg_errormessage() → pg_last_error()
- pg_numrows() → pg_num_rows()
- pg_numfields() → pg_num_fields()
- pg_cmdtuples() → pg_affected_rows()
- pg_fieldname() → pg_field_name()
- pg_fieldsize() → pg_field_size()
- pg_fieldtype() → pg_field_type()
- pg_fieldnum() → pg_field_num()
- pg_result() → pg_fetch_result()
- pg_fieldprtlen() → pg_field_prtlen()
- pg_fieldisnull() → pg_field_is_null()
- pg_freeresult() → pg_free_result()
- pg_getlastoid() → pg_last_oid()
- pg_locreate() → pg_lo_create()
- pg_lounlink() → pg_lo_unlink()
- pg_loopen() → pg_lo_open()
- pg_loclose() → pg_lo_close()
- pg_loread() → pg_lo_read()
- pg_lowrite() → pg_lo_write()
- pg_loreadall() → pg_lo_read_all()
- pg_loimport() → pg_lo_import()
- pg_loexport() → pg_lo_export()
- pg_setclientencoding() → pg_set_client_encoding()
- pg_clientencoding() -> pg_client_encoding()
标准库
Zip
-
弃用使用空文件作为 ZipArchive。Libzip 1.6.0 不再接受空文件作为有效的 zip 归档。现有的解决方案将在下个版本删除。
-
弃用面向过程的 Zip API。使用 ZipArchive 替代。可以使用 ZipArchive::statIndex() 和 for 循环完成对所有条目的迭代:
<?php
// 使用面向过程 API 进行迭代
assert(is_resource($zip));
while ($entry = zip_read($zip)) {
echo zip_entry_name($entry);
}
// 使用面向对象 API 进行迭代
assert($zip instanceof ZipArchive);
for ($i = 0; $entry = $zip->statIndex($i); $i++) {
echo $entry['name'];
}
?>
反射
-
弃用 ReflectionFunction::isDisabled(),因为不可能再为禁用的函数创建 ReflectionFunction。此方法现在总是返回
false
。 -
弃用 ReflectionParameter::getClass()、 ReflectionParameter::isArray()、 ReflectionParameter::isCallable()。 应该使用 ReflectionParameter::getType() 和 ReflectionType API 替代。
用户贡献的备注 1 note
If you try to get all methods / functions assigning an optional argument before a mandatory one, try this regex (single line)
<?php
function\s+[a-z][a-zA-Z0-9_]*\((?:\$[a-z][a-zA-Z0-9]*\s*,\s*)*
(?:\$[a-z][A-Za-z0-9_]*\s*=[^\$\)]+)+\$[a-z][a-zA-Z0-9_]*\)
?>
for
<?php
public function test($a, $b) {
$a = [];
$b = [$abc => $ss[],
];
}
private function too($c, $a = true, $b) {
}
protected function bar($a = []) {
}
public function foo($a, $b = true) {
}
public function fooBar32($a=true, $b = [], $c) {
}
private function oo_bAr($a = []) {
}
?>
it will match too() and fooBar32()
Have a nice migration! ;)
备份地址:http://www.lvesu.com/blog/php/migration80.deprecated.php