新特性

PHP 核心中的新特性

命名参数

新增命名参数的功能。

注解(Attributes)

新增注解的功能。

构造器属性提升(Constructor Property Promotion)

新增构造器属性提升功能 在构造函数中声明类的属性)。

联合类型

新增 联合类型

Match 表达式

新增 match 表达式

Nullsafe 运算符

新增Nullsafe 运算符(?->)。

其他新特性

  • 新增 WeakMap 类。

  • 新增 ValueError 类。

  • 现在,只要类型兼容,任意数量的函数参数都可以用一个可变参数替换。 例如允许编写下面的代码:

    <?php
    class A {
    public function
    method(int $many, string $parameters, $here) {}
    }
    class
    B extends A {
    public function
    method(...$everything) {}
    }
    ?>

  • static ("后期静态绑定"中) 可以作为返回类型:

    <?php
    class Test {
    public function
    create(): static {
    return new static();
    }
    }
    ?>

  • 现在可以通过 $object::class 获取类名,返回的结果和 get_class($object) 一致。

  • newinstanceof 可用于任何表达式, 用法为 new (expression)(...$args)$obj instanceof (expression)

  • 添加对一些变量语法一致性的修复,例如现在能够编写 Foo::BAR::$baz

  • 添加 Stringable interface, 当一个类定义 __toString() 方法后会自动实现该接口。

  • Trait 可以定义私有抽象方法(abstract private method)。 类必须实现 trait 定义的该方法。

  • 可作为表达式使用 throw。 使得可以编写以下用法:

    <?php
    $fn
    = fn() => throw new Exception('Exception in arrow function');
    $user = $session->user ?? throw new Exception('Must have user');

  • 参数列表中的末尾逗号为可选。

    <?php
    function functionWithLongSignature(
    Type1 $parameter1,
    Type2 $parameter2, // <-- 这个逗号也被允许了
    ) {
    }

  • 现在允许 catch (Exception) 一个 exception 而无需捕获到变量中。

  • 支持 mixed 类型。

  • 父类中声明的私有方法不在对子类中的方法执行任何继承规则(final private 构造函数除外)。下列示例说明删除了那些限制:

    <?php
    class ParentClass {
    private function
    method1() {}
    private function
    method2() {}
    private static function
    method3() {}
    // 抛出警告,因为“final”不再有效:
    private final function method4() {}
    }
    class
    ChildClass extends ParentClass {
    // 现在允许以下所有内容,即使修饰符与父类中的私有方法不同。
    public abstract function method1() {}
    public static function
    method2() {}
    public function
    method3() {}
    public function
    method4() {}
    }
    ?>

  • 新增 get_resource_id(),返回值跟 (int) $resource 相同。其在更清晰的 API 下提供了相同的功能。

  • 新增 InternalIterator

日期和时间

DOM

新增新遍历和操作 API:DOMParentNodeDOMChildNode

Filter

新增 FILTER_VALIDATE_BOOL,是 FILTER_VALIDATE_BOOLEAN 的别名。首选新名称,因为其使用规范类型名称。

Enchant

新增 enchant_dict_add()enchant_dict_is_added()LIBENCHANT_VERSION

FPM

新增新选项 pm.status_listen,允许从不同的端点(例如端口或者 UDS 文件)获取状态。这对于所有的子进程忙于处理长时间运行的请求时获取状态特别有用。

Hash

HashContext 对象现在可序列化。

国际化函数

新增 IntlDateFormatter::RELATIVE_FULLIntlDateFormatter::RELATIVE_LONGIntlDateFormatter::RELATIVE_MEDIUMIntlDateFormatter::RELATIVE_SHORT 常量。

LDAP

新增 ldap_count_references(),返回搜索结果中引用消息的数量。

OPcache

如果启用了 opcache.record_warnings ini 设置,OPcache 将在编译时记录警告并在下一次包含时重放,即使是从缓存中提供的。

OpenSSL

新增由加密、解密、签名、验证和读取功能组成的加密消息语法(CMS)(» RFC 5652)支持。该 API 类似于 PKCS #7 函数的 API,但增加了新的编码常量:OPENSSL_ENCODING_DEROPENSSL_ENCODING_SMIMEOPENSSL_ENCODING_PEM

  • openssl_cms_encrypt() 使用证书加密文件中的消息并将结果输出到提供的文件。
  • openssl_cms_decrypt() 解密文件中的 S/MIME 消息并将结果输出到提供的文件。
  • openssl_cms_read() 将 CMS 文件导出到 PEM 证书数组。
  • openssl_cms_sign() 使用证书和密钥对文件中的 MIME 消息进行签名,并将结果输出到提供的文件。
  • openssl_cms_verify() that verifies that the data block is intact, the signer is who they say they are, and returns the certs of the signers.

正则表达式(兼容 Perl)

新增 preg_last_error_msg(),返回最后一条人可读的 PCRE 错误消息。它是对 preg_last_error()(返回整数枚举值)的补充。

SQLite3

SQLite3::setAuthorizer() and respective class constants have been added to set a userland callback that will be used to authorize or not an action on the database.

标准库

  • 新增 str_contains()str_starts_with()str_ends_with(),分别检查 haystack 是否包含 needle 或以 needle 开头/结尾。

  • 新增 fdiv() 在 IEEE 754 语义下执行浮点除法。认为除以零已经明确定义,将返回 Inf-InfNaN

  • 新增 get_debug_type() 返回对错误消息有用的类型。与 gettype() 不同的是,它使用规范的类型名称,为对象返回类名,为资源表示资源类型。

  • printf() and friends now support the %h and %H format specifiers. These are the same as %g and %G, but always use "." as the decimal separator, rather than determining it through the LC_NUMERIC locale.

  • printf() and friends now support using "*" as width or precision, in which case the width/precision is passed as an argument to printf. This also allows using precision -1 with %g, %G, %h and %H. For example, the following code can be used to reproduce PHP's default floating point formatting:

    <?php
    printf
    ("%.*H", (int) ini_get("precision"), $float);
    printf("%.*H", (int) ini_get("serialize_precision"), $float);
    ?>

  • proc_open() now supports pseudo-terminal (PTY) descriptors. The following attaches stdin, stdout and stderr to the same PTY:

    <?php
    $proc
    = proc_open($command, [['pty'], ['pty'], ['pty']], $pipes);
    ?>

  • proc_open() now supports socket pair descriptors. The following attaches a distinct socket pair to stdin, stdout and stderr:

    <?php
    $proc
    = proc_open($command, [['socket'], ['socket'], ['socket']], $pipes);
    ?>

    Unlike pipes, sockets do not suffer from blocking I/O issues on Windows. However, not all programs may work correctly with stdio sockets.

  • 排序函数现在已稳定,这意味着相等的元素比较将保留其原始顺序。

  • array_diff()array_intersect() 及其变体现在可以接受单个数组作为参数。这意味着现在可以使用下列用法:

    <?php
    // 如果 $excludes 为空也可以:
    array_diff($array, ...$excludes);
    // 如果 $arrays 仅包含单个数组也可以:
    array_intersect(...$arrays);
    ?>

  • ob_implicit_flush()flag 参数已经从接受 int 变更为接受 bool

Tokenizer

PhpToken 为分词器(tokenizer)新增基于对象的接口。表现更为统一且符合人体工程学,同时内存效率更高、速度更快。

Zip

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top