对象属性现在可以其 get
和 set
操作中关联相关的附加逻辑。根据用法,这可能会也可能不会使属性变为虚拟属性,即该属性根本没有实际的存储值。
<?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 本身提供的现有弃用已更新为使用该属性,通过包含简短的解释来改进发出的错误消息。
添加 request_parse_body() 函数允许在非 POST HTTP 请求中解析 RFC1867(multipart)请求。
new
表达式不再需要括号具有构造函数参数的新表达式现在可解除引用(dereferencable),这意味着无需将表达式括在括号中,可以直接链接方法调用、属性访问等。
获取 WeakReference 的调试信息现在还会输出其引用的对象,如果引用不再有效,则输出 null
。
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_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
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\Node 是新的 DOMNode)。这些类与 HTML 5 兼容,并且符合 WHATWG 规范;解决了 DOM 扩展中长期存在的错误。旧的 DOM 类仍然可用,以实现向后兼容。
新增 DOMNode::compareDocumentPosition() 及其相关常量:
DOMNode::DOCUMENT_POSITION_DISCONNECTED
DOMNode::DOCUMENT_POSITION_PRECEDING
DOMNode::DOCUMENT_POSITION_FOLLOWING
DOMNode::DOCUMENT_POSITION_CONTAINS
DOMNode::DOCUMENT_POSITION_CONTAINED_BY
DOMNode::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC
现在可以将任何 callable 传递给
DOMXPath::registerPhpFunctions()。
此外,现在使用 DOMXPath::registerPhpFunctionNs() 可以注册使用原生函数调用语法,而不是使用
php:function('name')
。
Added the NumberFormatter::ROUND_HALFODD
to
complement the existing NumberFormatter::ROUND_HALFEVEN
functionality.
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.
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.
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:
Added a custom parser supporting:
Added a custom parser supporting:
E'string'
)
??
as escape sequence for the
?
operator
Added a custom parser supporting:
Added support for the Unix timestamp extension for Zip archives.
Added ability to change the .php_history
path through
the PHP_HISTFILE environment variable.
ReflectionAttribute now contains a name property to improve the debugging experience.
ReflectionClassConstant::__toString() and ReflectionProperty::__toString() now returns the attached doc comments.
添加了与惰性对象功能相关的多个新方法和常量:
ReflectionClass::SKIP_INITIALIZATION_ON_SERIALIZE
ReflectionClass::SKIP_DESTRUCTOR
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.
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.
Added the ZipArchive::ER_TRUNCATED_ZIP
constant, which was added in libzip 1.11.