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

LibXML

弃用 libxml_disable_entity_loader()。由于现在需要 libxml 2.9.0,默认情况下会禁用外部实体加载并且不再需要此函数来防止 XXE 攻击,除非使用(仍然易受攻击的) LIBXML_NOENT。在这种情况下,建议使用 libxml_set_external_entity_loader() 重构代码以抑制外部实体加载。

PGSQL / PDO PGSQL

标准库

  • 排序比较函数现在返回 true 或者 false 将会引发弃用警告,应替换为返回小于、等于或者大于零的 int 值。

    <?php
    // 替换
    usort($array, fn($a, $b) => $a > $b);
    // 为
    usort($array, fn($a, $b) => $a <=> $b);
    ?>

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'];
    }
    ?>

反射

add a note add a note

User Contributed Notes 1 note

up
1
dominique dot vienne at ik dot me
2 years ago
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! ;)
To Top