

PHP 8.4是PHP語(yǔ)言的主要更新。
它包含許多新功能,例如屬性鉤,不對(duì)稱的可見(jiàn)性,更新的DOM API,性能改進(jìn),錯(cuò)誤修復(fù)和一般清理。
現(xiàn)在升級(jí)到PHP 8.4!屬性鉤
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

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
屬性掛鉤為計(jì)算屬性提供了支持,這些屬性可以通過(guò)IDE和靜態(tài)分析工具本地理解,而無(wú)需編寫(xiě)可能失去同步的DocBlock評(píng)論。此外,它們?cè)试S對(duì)值的可靠預(yù)或后處理,而無(wú)需檢查班級(jí)中是否存在匹配的Getter或Setter。
不對(duì)稱的可見(jiàn)性
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}" ;
}
}

public private(set) string $version = '8.4' ;
public function increment(): void {
[ $major, $minor ] = explode( '.', $this->version );
$minor++;
$this->version = "{$major}.{$minor}" ;
}
}
現(xiàn)在可以獨(dú)立于讀取屬性的范圍來(lái)控制到屬性的范圍,從而減少了對(duì)樣板getter方法的需求,以公開(kāi)屬性的值,而不允許從類的外部進(jìn)行修改。
#[\Deprecated] 屬性
/**
* @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();

#[\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支持
$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)

<<<'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)
包括符合標(biāo)準(zhǔn)支持HTML5文檔的符合標(biāo)準(zhǔn)支持的新DOM API,在DOM功能的行為中修復(fù)了幾個(gè)長(zhǎng)期存在的合規(guī)性錯(cuò)誤,并添加了多個(gè)功能,以使使用文檔更方便。
新的DOM API可在DOM名稱空間內(nèi)使用。可以使用Domhtmldocument和DomxMldocument類創(chuàng)建使用新DOM API的文檔。
BCMATH的對(duì)象API
$num2 = '2';
$result = bcadd($num1, $num2, 5);
echo $result; // '2.12345'
var_dump(bccomp($num1, $num2) > 0); // false

$num1 = new Number('0.12345');
$num2 = new Number('2');
$result = $num1 + $num2;
echo $result; // '2.12345'
var_dump($num1 > $num2); // false
使用任意精度數(shù)字時(shí),新的BCMATHNUMBER對(duì)象可以啟用面向?qū)ο蟮挠梅ê蜆?biāo)準(zhǔn)數(shù)學(xué)運(yùn)算符。
這些對(duì)象是不可變的,并實(shí)現(xiàn)了可弦樂(lè)接口,因此可以在Echo $ num等字符串上下文中使用它們。
New array_*() functions
foreach ( [ 'dog', 'cat', 'cow', 'duck', 'goose' ] as $value ) {
if ( str_starts_with($value, 'c') ) {
$animal = $value;
break;
}
}
var_dump($animal); // string(3) "cat"

[ '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驅(qū)動(dòng)程序特定子類
'sqlite:foo.db',
$username,
$password,
); // object(PDO)
$connection->sqliteCreateFunction(
'prepend_php',
static fn ($string) => "PHP {$string}",
);
$connection->query('SELECT prepend_php(version) FROM php');

'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()沒(méi)有括號(hào)
public function getVersion(): string {
return 'PHP 8.4';
}
}
var_dump(( new PhpVersion())->getVersion());

public function getVersion(): string {
return 'PHP 8.4';
}
}
var_dump(new PhpVersion()->getVersion());
現(xiàn)在可以訪問(wèn)新實(shí)例化對(duì)象的屬性和方法,而無(wú)需將新表達(dá)式包裹在括號(hào)中。
新增類、接口和函數(shù)
- 新的懶惰對(duì)象。
- 基于IR框架的新JIT實(shí)施。
- 新request_parse_body()函數(shù)。
- 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()函數(shù)。
- new PCNTL_GETCPU(),PCNTL_GETCPUAFFINITY(),PCNTL_GETQOS_CLASS(),PCNTL_SETNS()和PCNTL_WAITID()函數(shù)。
- 新的ReflectionClassConstant :: ISDepRecated(),ReflectionGenerator :: isClosed()和ReflectionProperty :: Isdynamic()方法。
- 新的HTTP_GET_LAST_RESPONSE_HEADERS(),http_clear_last_response_headers()和fpow()函數(shù)。
- 新的XMLReader :: Fromstream(),XmlReader :: Fromuri(),XmlReader :: fromstring(),XMLWRITER :: tostream(),XMLWRITER :: touri()和XMLWriter()和XMLWRITER :: tomemory(tomemory(tomemory)方法。
- 新grupheme_str_split()函數(shù)。
棄用和向后兼容性中斷
- IMAP, OCI8, PDO_OCI, and pspell extensions have been unbundled and moved to PECL.
- 隱式無(wú)效的參數(shù)類型現(xiàn)在已棄用。
- 現(xiàn)在使用_用作班級(jí)名稱。
- 將零提高到負(fù)數(shù)的功率現(xiàn)在已貶值。
- 將無(wú)效模式傳遞給圓()拋出valueerror。
- 現(xiàn)在鍵入擴(kuò)展日期,INTL,PDO,REFLECTION,SPL,SQLITE,XMLREEDER的類常數(shù)。
- GMP課程現(xiàn)在是最終的。
- mysqli_set_charset_dir,mysqli_stmt_attr_prefetch_rows,mysqli_cursor_type_for_update,mysqli_cursor_type_scrollable,mysqli_type_interval常數(shù)已被撤消。
- mysqli_ping(),mysqli_kill(),mysqli_refresh()函數(shù),mysqli :: ping(),mysqli :: kill(kill(),mysqli :: recresh(refresh()方法,mysqli_refresh_*常數(shù)已被execectedeced。
- stream_bucket_make_writable()和stream_bucket_new()現(xiàn)在返回streambucket的實(shí)例而不是stdclass。
- 退出()行為改變。
- e_strict常數(shù)已被棄用。
有關(guān)PHP 8.4的源下載,請(qǐng)?jiān)L問(wèn) 下載 頁(yè)。 Windows二進(jìn)制文件可以在 Windows的PHP 地點(diǎn)。更改列表記錄在 ChangElog。
這 遷移指南 可在PHP手冊(cè)中使用。請(qǐng)咨詢以獲取新功能和向后不兼容的更改的詳細(xì)列表。