新功能

PHP 核心

属性挂钩

对象属性现在可以其 getset 操作中关联相关的附加逻辑。根据用法,这可能会也可能不会使属性变为虚拟属性,即该属性根本没有实际的存储值。

<?php
class Person
{
// “虚拟”属性,可能无法明确设置。
public string $fullName {
get => $this->firstName . ' ' . $this->lastName;
}

// 所有的写入操作都会经过这个挂钩,结果就是写入的内容。
// 读取访问正常。
public string $firstName {
set => ucfirst(strtolower($value));
}

// 所有的写入操作都会经过这个挂钩,它必须写入支持值本身。
// 读取访问正常。
public string $lastName {
set {
if (
strlen($value) < 2) {
throw new
\InvalidArgumentException('Too short');
}
$this->lastName = $value;
}
}
}

$p = new Person();

$p->firstName = 'peter';
print
$p->firstName; // 打印“Peter”
$p->lastName = 'Peterson';
print
$p->fullName; // 打印“Peter Peterson”

不对称属性可见性

现在可以将对象属性的 set 可见性和 get 可见性分开控制。

<?php
class Example
{
// 第一个可见性修饰符控制 get 可见性,第二个修饰符控制 set 可见性。
// The get-visibility must not be narrower than set-visibility.
public protected(set) string $name;

public function
__construct(string $name)
{
$this->name = $name;
}
}

惰性对象

现在可以创建对象,将初始化延迟到访问时。库和框架可以利用这些惰性对象来延迟获取初始化所需的数据或依赖项。

<?php
class Example
{
public function
__construct(private int $data)
{
}

// ...
}

$initializer = static function (Example $ghost): void {
// 获取数据或者依赖项
$data = ...;
// 初始化
$ghost->__construct($data);
};

$reflector = new ReflectionClass(Example::class);
$object = $reflector->newLazyGhost($initializer);

#[\Deprecated] 注解

新的 Deprecated 属性可用于将函数、方法和类常量标记为已弃用。此弃用属性的行为与 PHP 本身提供的现有弃用机制的行为一致。唯一的例外是发出的错误代码是 E_USER_DEPRECATED,而不是 E_DEPRECATED

PHP 本身提供的现有弃用已更新为使用该属性,通过包含简短的解释来改进发出的错误消息。

在非 POST HTTP 请求中解析 RFC1867(multipart)请求

添加 request_parse_body() 函数允许在非 POST HTTP 请求中解析 RFC1867(multipart)请求。

链接 new 表达式不再需要括号

具有构造函数参数的新表达式现在可解除引用(dereferencable),这意味着无需将表达式括在括号中,可以直接链接方法调用、属性访问等。

改进了 WeakReference 的调试信息

获取 WeakReference 的调试信息现在还会输出其引用的对象,如果引用不再有效,则输出 null

改进了 Closure 的调试信息

Closure::__debugInfo() 的输出现在包含 Closure 的名称、文件名、和行数。

Defining Identical Symbols in Different Namespace Blocks

Exiting a namespace now clears seen symbols. This allows using a symbol in a namespace block, even if a previous namespace block declared a symbol with the same name.

cURL

curl_version() 返回额外的 feature_list 值,是所有已知 CURL 功能的滚脸上数组,值是支持(true)或者不支持(false)。

Added CURL_HTTP_VERSION_3 and CURL_HTTP_VERSION_3ONLY constants (available since libcurl 7.66 and 7.88) as available options for CURLOPT_HTTP_VERSION.

Added CURLOPT_PREREQFUNCTION as a cURL option that accepts a callable to be called after the connection is made, but before the request is sent. This callable must return either CURL_PREREQFUNC_OK or CURL_PREREQFUNC_ABORT to allow or abort the request.

Added CURLOPT_SERVER_RESPONSE_TIMEOUT, which was formerly known as CURLOPT_FTP_RESPONSE_TIMEOUT. Both constants hold the same value.

Added CURLOPT_DEBUGFUNCTION as a cURL option that accepts a callable that gets called during the request lifetime with the CurlHandle object, an integer containing the debug message type, and a string containing the debug message. The debug message type is one of the following constants:

  • CURLINFO_TEXT
  • CURLINFO_HEADER_IN
  • CURLINFO_HEADER_OUT
  • CURLINFO_DATA_IN
  • CURLINFO_DATA_OUT
  • CURLINFO_SSL_DATA_IN
  • CURLINFO_SSL_DATA_OUT
Once this option is set, CURLINFO_HEADER_OUT must not be set because it uses the same libcurl functionality.

The curl_getinfo() now returns an additional posttransfer_time_us key, containing the number of microseconds from the start until the last byte is sent. When a redirect is followed, the time from each request is added together. This value can also be retrieved by passing CURLINFO_POSTTRANSFER_TIME_T to the curl_getinfo() option parameter. This requires libcurl 8.10.0 or later.

DOM

添加了 Dom 命名空间,其中包含与现有 DOM 类对应的新类(例如,Dom\Node 是新的 DOMNode)。这些类与 HTML 5 兼容,并且符合 WHATWG 规范;解决了 DOM 扩展中长期存在的错误。旧的 DOM 类仍然可用,以实现向后兼容。

新增 DOMNode::compareDocumentPosition() 及其相关常量:

现在可以将任何 callable 传递给 DOMXPath::registerPhpFunctions()。 此外,现在使用 DOMXPath::registerPhpFunctionNs() 可以注册使用原生函数调用语法,而不是使用 php:function('name')

Intl

Added the NumberFormatter::ROUND_HALFODD to complement the existing NumberFormatter::ROUND_HALFEVEN functionality.

OpenSSL

Added support for Curve25519 + Curve448 based keys. Specifically x25519, ed25519, x448 and ed448 fields are supported in openssl_pkey_new(), openssl_pkey_get_details(), openssl_sign(), and openssl_verify() were extended to support those keys.

Implement PASSWORD_ARGON2 password hashing. Requires OpenSSL 3.2 and NTS build.

PCRE

The bundled pcre2lib has been updated to version 10.44. As a consequence, LoongArch JIT support has been added, spaces are now allowed between braces in Perl-compatible items, and variable-length lookbehind assertions are now supported.

With pcre2lib version 10.44, the maximum length of named capture groups has changed from 32 to 128.

Added support for the r (PCRE2_EXTRA_CASELESS_RESTRICT) modifier, as well as the (?r) mode modifier. When enabled along with the case-insensitive modifier (i), the expression locks out mixing of ASCII and non-ASCII characters.

PDO

Added support for driver-specific subclasses. This RFC adds subclasses for PDO in order to better support database-specific functionalities. The new classes are instantiatable either via calling the PDO::connect() method or by instantiating an instance of the driver-specific subclass directly.

Added support for driver specific SQL parsers. The default parser supports:

  • single and double-quoted literals, with doubling as escaping mechanism
  • two-dashes and non-nested C-style comments

PDO_MYSQL

Added a custom parser supporting:

  • single and double-quoted literals, with doubling and backslash as escaping mechanism
  • backtick literal identifiers and with doubling as escaping mechanism
  • two dashes followed by at least 1 whitespace, non-nested C-style comments, and hash-comments

PDO_PGSQL

Added a custom parser supporting:

  • single and double-quoted literals, with doubling as escaping mechanism
  • C-style "escape" string literals (E'string')
  • dollar-quoted string literals
  • two-dashes and C-style comments (non-nested)
  • support for ?? as escape sequence for the ? operator

PDO_SQLITE

Added a custom parser supporting:

  • single, double-quoted, and backtick literals, with doubling as escaping mechanism
  • square brackets quoting for identifiers
  • two-dashes and C-style comments (non-nested)

Phar

Added support for the Unix timestamp extension for Zip archives.

Readfile

Added ability to change the .php_history path through the PHP_HISTFILE environment variable.

SOAP

Added support for clark notation for namespaces in class map. It is now possible to specify entries in a class map with clark notation to resolve a type with a specific namespace to a specific class. For example: '{http://example.com}foo' => 'FooClass'.

Instances of DateTimeInterface that are passed to xsd:datetime or similar elements are now serialized as such instead of being serialized as an empty string.

Session persistence now works with a shared session module.

标准

Added a new RoundingMode enum with clearer naming and improved discoverability compared to the PHP_ROUND_* constants. Moreover, four new rounding modes were added which are only available via the new RoundingMode enum.

XSL

It is now possible to use parameters that contain both single and double quotes.

It is now possible to pass any callable to XSLTProcessor::registerPhpFunctions().

Added XSLTProcessor::$maxTemplateDepth and XSLTProcessor::$maxTemplateVars to control the recursion depth of XSL template evaluation.

Zip

Added the ZipArchive::ER_TRUNCATED_ZIP constant, which was added in libzip 1.11.

添加备注

用户贡献的备注

此页面尚无用户贡献的备注。
To Top