软件大小:20.62 MB
软件语言:简体中文
软件授权:免费版
软件类别:编程开发
软件等级:
更新时间:2024-11-22
官方网站:
应用平台:/Win10/Win7/WinXP
- 软件介绍
- 软件截图
- 相关阅读
- 网友评论
- 下载地址
PHP 8.4 是 PHP 编程语言发展历程中的关键一步,相较于以往版本实现了多维度的显著进化。与早期版本相比,其性能有了质的飞跃,通过深度优化核心代码逻辑,大幅减少了执行时间并优化了内存使用,在高并发场景下表现更为出色。例如,相较于 PHP 7.x 系列,某些复杂业务逻辑的处理速度提升了 [X]%,内存占用降低了 [Y]%。
在语法特性方面,对比 PHP 5.x 等旧版本,PHP 8.4 引入了诸多创新且实用的语法糖。如数组解构功能得到极大增强,开发者可以更轻松地从数组中提取并赋值数据,减少了冗余代码量;新的 match 表达式扩展相较于传统的 switch 语句更加智能高效,其模式匹配能力不仅提升了代码的可读性,还降低了编写复杂条件判断逻辑时出错的概率。
类型系统上,PHP 8.4 较之前版本持续拓展和细化,相比 PHP 7.0 初步引入类型声明时的有限支持,如今能够在更多的代码场景中进行精确的类型约束,如函数参数、返回值以及类属性等,有效增强了代码的健壮性和可维护性,让开发者能在开发阶段更早地发现潜在类型错误。
无论是开发大型企业级复杂应用,还是构建小型的个人网站项目,PHP 8.4 凭借其相较于其他版本在性能、语法和类型系统等方面的卓越改进,都为开发者提供了更为先进、高效且可靠的编程利器,极大地推动了软件应用开发的效率与质量提升。
PHP 8.4 是 PHP 语言的一次重大更新。它包含许多新功能,例如属性钩子、不对称可见性、更新的 DOM API、性能改进、错误修复和常规清理等。
属性钩子
属性钩子提供对计算属性的支持,这些属性可以被 IDE 和静态分析工具直接理解,而无需编写可能会失效的 docblock 注释。此外,它们允许可靠地预处理或后处理值,而无需检查类中是否存在匹配的 getter 或 setter。
PHP < 8.4
class Locale
{
private string $languageCode;
private string $countryCode;
public function __construct(string $languageCode, string $countryCode)
{
$this->setLanguageCode($languageCode);
$this->setCountryCode($countryCode);
}
public function getLanguageCode(): string
{
return $this->languageCode;
}
public function setLanguageCode(string $languageCode): void
{
$this->languageCode = $languageCode;
}
public function getCountryCode(): string
{
return $this->countryCode;
}
public function setCountryCode(string $countryCode): void
{
$this->countryCode = strtoupper($countryCode);
}
public function setCombinedCode(string $combinedCode): void
{
[$languageCode, $countryCode] = explode('_', $combinedCode, 2);
$this->setLanguageCode($languageCode);
$this->setCountryCode($countryCode);
}
public function getCombinedCode(): string
{
return \sprintf("%s_%s", $this->languageCode, $this->countryCode);
}
}
$brazilianPortuguese = new Locale('pt', 'br');
var_dump($brazilianPortuguese->getCountryCode()); // BR
var_dump($brazilianPortuguese->getCombinedCode()); // pt_BR
PHP 8.4
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
方法来公开属性值而不允许从类外部修改属性的需求。
PHP < 8.4
class PhpVersion
{
public string $version = '8.3';
}
$phpVersion = new PhpVersion();
var_dump($phpVersion->version); // string(3) "8.3"
$phpVersion->version = 'PHP 8.4'; // No error
PHP 8.4
class PhpVersion
{
public private(set) string $version = '8.4';
}
$phpVersion = new PhpVersion();
var_dump($phpVersion->version); // string(3) "8.4"
$phpVersion->version = 'PHP 8.3'; // Visibility error
#[\Deprecated] 属性
新的
#[\Deprecated]
属性使 PHP 的现有弃用机制可用于用户定义的函数、方法和类常量。
PHP < 8.4
class PhpVersion
{
/**
* @deprecated 8.3 use PhpVersion::getVersion() instead
*/
public function getPhpVersion(): string
{
return $this->getVersion();
}
public function getVersion(): string
{
return '8.3';
}
}
$phpVersion = new PhpVersion();
// No indication that the method is deprecated.
echo $phpVersion->getPhpVersion();
PHP 8.4
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
类创建文档。
PHP < 8.4
$dom = new DOMDocument();
$dom->loadHTML(
<<<'HTML'
<main>
<article>PHP 8.4 is a feature-rich release!</article>
<article class="featured">PHP 8.4 adds new DOM classes that are spec-compliant, keeping the old ones for compatibility.</article>
</main>
HTML,
LIBXML_NOERROR,
);
$xpath = new DOMXPath($dom);
$node = $xpath->query(".//main/article[not(following-sibling::*)]")[0];
$classes = explode(" ", $node->className); // Simplified
var_dump(in_array("featured", $classes)); // bool(true)
PHP 8.4
$dom = Dom\HTMLDocument::createFromString(
<<<HTML
<main>
<article>PHP 8.4 is a feature-rich release!</article>
<article class="featured">PHP 8.4 adds new DOM classes that are spec-compliant, keeping the old ones for compatibility.</article>
</main>
HTML,
LIBXML_NOERROR,
);
$node = $dom->querySelector('main > article:last-child');
var_dump($node->classList->contains("featured")); // bool(true)
新的 array_*()
函数
新增函数
array_find()
、array_find_key()
、array_any()
和array_all()
。
PHP < 8.4
$animal = null;
foreach (['dog', 'cat', 'cow', 'duck', 'goose'] as $value) {
if (str_starts_with($value, 'c')) {
$animal = $value;
break;
}
}
var_dump($animal); // string(3) "cat"
PHP 8.4
$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\Sqlite
的子类可用。
PHP < 8.4
$connection = new PDO(
'sqlite:foo.db',
$username,
$password,
); // object(PDO)
$connection->sqliteCreateFunction(
'prepend_php',
static fn ($string) => "PHP {$string}",
);
$connection->query('SELECT prepend_php(version) FROM php');
PHP 8.4
$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
表达式的情况下访问新实例化对象的属性和方法。
PHP < 8.4
class PhpVersion
{
public function getVersion(): string
{
return 'PHP 8.3';
}
}
var_dump((new PhpVersion())->getVersion());
PHP 8.4
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
常量已弃用。
- 下载地址