PHP 8.4 是 PHP 语言的一次重大更新。
它包含许多新功能,例如属性钩子、不对称可见性、更新的 DOM API、性能改进、错误修复和常规清理等。
属性钩子
属性钩子提供对计算属性的支持,这些属性可以被 IDE 和静态分析工具直接理解,而无需编写可能会失效的 docblock 注释。此外,它们允许可靠地预处理或后处理值,而无需检查类中是否存在匹配的 getter 或 setter。
class Locale
{
public string $languageCode;
public string $countryCode
{
set (string $countryCode) {
$this->countryCode = strtoupper($countryCode);
}
}
public string $combinedCode
{
get => \sprintf("%s_%s", $this->languageCode, $this->countryCode);
set (string $value) {
[$this->countryCode, $this->languageCode] = explode('_', $value, 2);
}
}
public function __construct(string $languageCode, string $countryCode)
{
$this->languageCode = $languageCode;
$this->countryCode = $countryCode;
}
}
$brazilianPortuguese = new Locale('pt', 'br');
var_dump($brazilianPortuguese->countryCode); // BR
var_dump($brazilianPortuguese->combinedCode); // pt_BR
不对称可见性
现在可以独立地控制写入属性的作用域和读取属性的作用域,减少了需要编写繁琐的 getter 方法来公开属性值而不允许从类外部修改属性的需求。
新的 #[\Deprecated] 属性使 PHP 的现有弃用机制可用于用户定义的函数、方法和类常量。
class PhpVersion
{
#[\Deprecated(
message: "use PhpVersion::getVersion() instead",
since: "8.4",
)]
public function getPhpVersion(): string
{
return $this->getVersion();
}
public function getVersion(): string
{
return '8.4';
}
}
$phpVersion = new PhpVersion();
// Deprecated: Method PhpVersion::getPhpVersion() is deprecated since 8.4, use PhpVersion::getVersion() instead
echo $phpVersion->getPhpVersion();
新的 ext-dom 功能和 HTML5 支持
新的 DOM API 包括符合标准的支持,用于解析 HTML5 文档,修复了 DOM 功能行为中的几个长期存在的规范性错误,并添加了几个函数,使处理文档更加方便。
新的 DOM API 可以在 Dom 命名空间中使用。使用新的 DOM API 可以使用 Dom\HTMLDocument 和 Dom\XMLDocument 类创建文档。
use BcMath\Number;
$num1 = new Number('0.12345');
$num2 = new Number('2');
$result = $num1 + $num2;
echo $result; // '2.12345'
var_dump($num1 > $num2); // false
新的 array_*() 函数
新增函数 array_find()、array_find_key()、array_any() 和 array_all()。
$animal = array_find(
[‘dog’, ‘cat’, ‘cow’, ‘duck’, ‘goose’],
static fn (string $value): bool => str_starts_with($value, ‘c’),
);
var_dump($animal); // string(3) “cat”
PDO 驱动程序特定的 SQL 解析器
新的 Pdo\Dblib、Pdo\Firebird、Pdo\MySql、Pdo\Odbc、Pdo\Pgsql 和 Pdo\Sqlite 的子类可用。
$connection = PDO::connect(
'sqlite:foo.db',
$username,
$password,
); // object(Pdo\Sqlite)
$connection->createFunction(
'prepend_php',
static fn ($string) => "PHP {$string}",
); // Does not exist on a mismatching driver.
$connection->query('SELECT prepend_php(version) FROM php');
new MyClass()->method() 不需要括号
现在可以在不使用括号包裹 new 表达式的情况下访问新实例化对象的属性和方法。
class PhpVersion
{
public function getVersion(): string
{
return 'PHP 8.4';
}
}
var_dump(new PhpVersion()->getVersion());
新的类、接口和函数
- 新的 延迟对象。
- 基于 IR 框架的新 JIT 实现。
- 新增
request_parse_body()函数。 - 新增
bcceil()、bcdivmod()、bcfloor()和bcround()函数。 - 新增
RoundingMode枚举用于round(),包括 4 个新的舍入模式TowardsZero、AwayFromZero、NegativeInfinity和PositiveInfinity。 - 新增
DateTime::createFromTimestamp()、DateTime::getMicrosecond()、DateTime::setMicrosecond()、DateTimeImmutable::createFromTimestamp()、DateTimeImmutable::getMicrosecond()和DateTimeImmutable::setMicrosecond()方法。 - 新增
mb_trim()、mb_ltrim()、mb_rtrim()、mb_ucfirst()和mb_lcfirst()函数。 - 新增
pcntl_getcpu()、pcntl_getcpuaffinity()、pcntl_getqos_class()、pcntl_setns()和pcntl_waitid()函数。 - 新增
ReflectionClassConstant::isDeprecated()、ReflectionGenerator::isClosed()和ReflectionProperty::isDynamic()方法。 - 新增
http_get_last_response_headers()、http_clear_last_response_headers()和fpow()函数。 - 新增
XMLReader::fromStream()、XMLReader::fromUri()、XMLReader::fromString()、XMLWriter::toStream()、XMLWriter::toUri()和XMLWriter::toMemory()方法。 - 新增
grapheme_str_split()函数。
弃用和向后不兼容
- IMAP、OCI8、PDO_OCI 和 pspell 扩展已从 PHP 中分离并移至 PECL。
- 隐式可空参数类型现已弃用。
- 使用
_作为类名现已弃用。 - 将零的负数次幂现已弃用。
- 向
round()传递无效模式将抛出ValueError。 - 来自扩展
date、intl、pdo、reflection、spl、sqlite、xmlreader的类常量现在是有类型的。 GMP类现已是 final 类。- 已删除
MYSQLI_SET_CHARSET_DIR、MYSQLI_STMT_ATTR_PREFETCH_ROWS、MYSQLI_CURSOR_TYPE_FOR_UPDATE、MYSQLI_CURSOR_TYPE_SCROLLABLE和MYSQLI_TYPE_INTERVAL常量。 - 已弃用
mysqli_ping()、mysqli_kill()、mysqli_refresh()函数,mysqli::ping()、mysqli::kill()、mysqli::refresh()方法,以及MYSQLI_REFRESH_*常量。 stream_bucket_make_writeable()和stream_bucket_new()现在返回StreamBucket实例而不是stdClass。exit()行为变更。E_STRICT常量已弃用。
更新到PHP 8.4
请访问 下载 页面下载 PHP 8.4 源代码。 在 PHP for Windows 站点中可找到 Windows 二进制文件。 ChangeLog 中有变更历史记录清单。
PHP 手册中有 迁移指南。 请参考它描述的新功能详细清单、向后不兼容的变化。
近期评论