下载 PHP 最新版本
更多变更日志
PHP 8.4是PHP语言的主要更新。
它包含许多新功能,例如属性钩,不对称的可见性,更新的DOM API,性能改进,错误修复和一般清理。
现在升级到PHP 8.4!
属性钩
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->languageCode,
$this->countryCode
]
=
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
属性挂钩为计算属性提供了支持,这些属性可以通过IDE和静态分析工具本地理解,而无需编写可能失去同步的DocBlock评论。此外,它们允许对值的可靠预或后处理,而无需检查班级中是否存在匹配的Getter或Setter。
不对称的可见性
PHP < 8.4
class
PhpVersion
{
private
string
$version
=
'8.3'
;
public function
getVersion():
string
{
return
$this->version;
}
public function
increment():
void
{
[
$major,
$minor
]
=
explode(
'.',
$this->version
);
$minor++;
$this->version
=
"{$major}.{$minor}"
;
}
}
PHP 8.4
class
PhpVersion
{
public private(set) string
$version
=
'8.4'
;
public function
increment():
void
{
[
$major,
$minor
]
=
explode(
'.',
$this->version
);
$minor++;
$this->version
=
"{$major}.{$minor}"
;
}
}
现在可以独立于读取属性的范围来控制到属性的范围,从而减少了对样板getter方法的需求,以公开属性的值,而不允许从类的外部进行修改。
#[\Deprecated] 属性
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();
The new #[Deprecated] attribute makes PHP’s existing deprecation mechanism available to user-defined functions, methods, and class constants.
新的Ext-DOM功能和HTML5支持
PHP < 8.4
$dom
=
new
DOMDocument();
$dom->loadHTML(
<<<'HTML'
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'
HTML,
LIBXML_NOERROR,
);
$node
=
$dom->querySelector('main > article:last-child');
var_dump($node->classList->contains("featured"));
// bool(true)
包括符合标准支持HTML5文档的符合标准支持的新DOM API,在DOM功能的行为中修复了几个长期存在的合规性错误,并添加了多个功能,以使使用文档更方便。
新的DOM API可在DOM名称空间内使用。可以使用Domhtmldocument和DomxMldocument类创建使用新DOM API的文档。
BCMATH的对象API
PHP < 8.4
$num1
=
'0.12345';
$num2
=
'2';
$result
=
bcadd($num1, $num2, 5);
echo
$result;
// '2.12345'
var_dump(bccomp($num1, $num2) > 0);
// false
PHP 8.4
use
BcMath\Number;
$num1
=
new
Number('0.12345');
$num2
=
new
Number('2');
$result
=
$num1
+
$num2;
echo
$result;
// '2.12345'
var_dump($num1 > $num2);
// false
使用任意精度数字时,新的BCMATHNUMBER对象可以启用面向对象的用法和标准数学运算符。
这些对象是不可变的,并实现了可弦乐接口,因此可以在Echo $ num等字符串上下文中使用它们。
New array_*() functions
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"
新功能array_find(),array_find_key(),array_any()和array_all()可用。
PDO驱动程序特定子类
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');
可以提供新的子类Pdodblib,Pdofirebird,PdomySQL,PDOODBC,PDOPGSQL和PDO的PDOSQLITE。
new MyClass()->method()没有括号
PHP < 8.4
class
PhpVersion
{
public function getVersion(): string
{
return
'PHP 8.4';
}
}
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()函数。
New bcceil(),>bcdivmod(),bcfloor(), and bcround() functions.
New RoundingMode enum for round() with 4 new rounding modes TowardsZero,AwayFromZero, NegativeInfinity,and PositiveInfinity.
new DateTime :: createfromtimestamp(),dateTime :: getmicrosecond(),dateTime :: setmicrosecond(),dateTimeImmutable :: createfromtimestamp(),dateTimeImmutable(),dateTimeImmutable()
new mb_trim(),mb_ltrim(),mb_rtrim(),mb_ucfirst()和mb_lcfirst()函数。
new 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()和XMLWRITER :: tomemory(tomemory(tomemory)方法。
新grupheme_str_split()函数。
弃用和向后兼容性中断
IMAP, OCI8, PDO_OCI, and pspell extensions have been unbundled and moved to PECL.
隐式无效的参数类型现在已弃用。
现在使用_用作班级名称。
将零提高到负数的功率现在已贬值。
将无效模式传递给圆()抛出valueerror。
现在键入扩展日期,INTL,PDO,REFLECTION,SPL,SQLITE,XMLREEDER的类常数。
GMP课程现在是最终的。
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(kill(),mysqli :: recresh(refresh()方法,mysqli_refresh_*常数已被execectedeced。
stream_bucket_make_writable()和stream_bucket_new()现在返回streambucket的实例而不是stdclass。
退出()行为改变。
e_strict常数已被弃用。
更好的性能、更好的语法、改进的类型安全性。 现在升级到PHP 8.4!
有关PHP 8.4的源下载,请访问 下载 页。 Windows二进制文件可以在 Windows的PHP 地点。更改列表记录在 ChangElog。
这 迁移指南 可在PHP手册中使用。请咨询以获取新功能和向后不兼容的更改的详细列表。