Migration80/deprecated-Phpdoc专题

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) {}       // 推荐写法
    ?>
  • Calling get_defined_functions with exclude_disabled explicitly set to false is deprecated and no longer has an effect. <span class="function">get_defined_functions will never include disabled functions.

Enchant

  • enchant_broker_set_dict_path and enchant_broker_get_dict_path are deprecated, because that functionality is neither available in libenchant \< 1.5 nor in libenchant-2.

  • enchant_dict_add_to_personal is deprecated; use enchant_dict_add instead.

  • enchant_dict_is_in_session is deprecated; use <span class="function">enchant_dict_is_added instead.

  • enchant_broker_free and <span class="function">enchant_broker_free_dict are deprecated; unset the object instead.

  • The ENCHANT_MYSPELL and ENCHANT_ISPELL constants are deprecated.

LibXML

libxml_disable_entity_loader has been deprecated. As libxml 2.9.0 is now required, external entity loading is guaranteed to be disabled by default, and this function is no longer needed to protect against XXE attacks.

PGSQL / PDO PGSQL

  • The constant PG_VERSION_STR now has the same value as PG_VERSION, and thus is deprecated.

  • Function aliases in the pgsql extension have been deprecated. See the following list for which functions should be used instead:

    • pg_errormessage → <span class="function">pg_last_error
    • pg_numrows → <span class="function">pg_num_rows
    • pg_numfields → <span class="function">pg_num_fields
    • pg_cmdtuples → <span class="function">pg_affected_rows
    • pg_fieldname → <span class="function">pg_field_name
    • pg_fieldsize → <span class="function">pg_field_size
    • pg_fieldtype → <span class="function">pg_field_type
    • pg_fieldnum → <span class="function">pg_field_num
    • pg_result → <span class="function">pg_fetch_result
    • pg_fieldprtlen → <span class="function">pg_field_prtlen
    • pg_fieldisnull → <span class="function">pg_field_is_null
    • pg_freeresult → <span class="function">pg_free_result
    • pg_getlastoid → <span class="function">pg_last_oid
    • pg_locreate → <span class="function">pg_lo_create
    • pg_lounlink → <span class="function">pg_lo_unlink
    • pg_loopen → <span class="function">pg_lo_open
    • pg_loclose → <span class="function">pg_lo_close
    • pg_loread → <span class="function">pg_lo_read
    • pg_lowrite → <span class="function">pg_lo_write
    • pg_loreadall → <span class="function">pg_lo_read_all
    • pg_loimport → <span class="function">pg_lo_import
    • pg_loexport → <span class="function">pg_lo_export
    • pg_setclientencoding → <span class="function">pg_set_client_encoding
    • pg_clientencoding -> <span class="function">pg_client_encoding

Standard Library

  • Sort comparison functions that return true or false will now throw a deprecation warning, and should be replaced with an implementation that returns an integer less than, equal to, or greater than zero.

    <?php
    // Replace
    usort($array, fn($a, $b) => $a > $b);
    // With
    usort($array, fn($a, $b) => $a <=> $b);
    ?>

Zip

  • Using an empty file as ZipArchive is deprecated. Libzip 1.6.0 does not accept empty files as valid zip archives any longer. The existing workaround will be removed in the next version.

  • The procedural API of Zip is deprecated. Use <span class="classname">ZipArchive instead. Iteration over all entries can be accomplished using <span class="methodname">ZipArchive::statIndex and a for loop:

    <?php
    // iterate using the procedural API
    assert(is_resource($zip));
    while ($entry = zip_read($zip)) {
        echo zip_entry_name($entry);
    }
    
    // iterate using the object-oriented API
    assert($zip instanceof ZipArchive);
    for ($i = 0; $entry = $zip->statIndex($i); $i++) {
        echo $entry['name'];
    }
    ?>

Reflection

  • ReflectionFunction::isDisabled is deprecated, as it is no longer possible to create a <span class="classname">ReflectionFunction for a disabled function. This method now always returns false.

  • ReflectionParameter::getClass, <span class="methodname">ReflectionParameter::isArray, and <span class="methodname">ReflectionParameter::isCallable are deprecated. <span class="methodname">ReflectionParameter::getType and the <span class="classname">ReflectionType APIs should be used instead.


本站为非盈利网站,作品由网友提供上传,如无意中有侵犯您的版权,请联系删除