
屬性鉤
privatestring$languageCode;
privatestring$countryCode;
public function__construct(string$languageCode,string$countryCode)
{
$this->setLanguageCode($languageCode);
$this->setCountryCode($countryCode);
}
public functiongetLanguageCode():string{
return$this->languageCode;
}
public functionsetLanguageCode(string$languageCode):void{
$this->languageCode=$languageCode;
}
public functiongetCountryCode():string{
return$this->countryCode;
}
public functionsetCountryCode(string$countryCode):void{
$this->countryCode=strtoupper($countryCode);
}
public functionsetCombinedCode(string$combinedCode):void{
[$languageCode,$countryCode]=explode('_',$combinedCode,2);
$this->setLanguageCode($languageCode);
$this->setCountryCode($countryCode);
}
public functiongetCombinedCode():string{
return\sprintf("%s_%s",$this->languageCode,$this->countryCode);
}
}
$brazilianPortuguese=newLocale('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=newLocale('pt','br');
var_dump($brazilianPortuguese->countryCode);// BR
var_dump($brazilianPortuguese->combinedCode);// pt_BR
屬性掛鉤為計算屬性提供了支持,這些屬性可以通過IDE和靜態分析工具本地理解,而無需編寫可能失去同步的DocBlock評論。此外,它們允許對值的可靠預或後處理,而無需檢查班級中是否存在匹配的Getter或Setter。
不對稱的可見性
privatestring$version='8.3';
public functiongetVersion():string{
return$this->version;
}
public functionincrement():void{
[$major,$minor]=explode('.',$this->version);
$minor++;
$this->version="{$major}.{$minor}";
}
}

public private(set) string$version='8.4';
public functionincrement():void{
[$major,$minor]=explode('.',$this->version);
$minor++;
$this->version="{$major}.{$minor}";
}
}
現在可以獨立於讀取屬性的範圍來控製到屬性的範圍,從而減少了對樣板getter方法的需求,以公開屬性的值,而不允許從類的外部進行修改。
#[Deprecated] Attribute
/**
* @deprecated 8.3 use PhpVersion::getVersion() instead
*/
public functiongetPhpVersion():string{
return$this->getVersion();
}
public functiongetVersion():string{
return'8.3';
}
}
$phpVersion=newPhpVersion();
// No indication that the method is deprecated.
echo$phpVersion->getPhpVersion();

#[\Deprecated(
message:"use PhpVersion::getVersion() instead",
since:"8.4",
)]
public functiongetPhpVersion():string{
return$this->getVersion();
}
public functiongetVersion():string{
return'8.4';
}
}
$phpVersion=newPhpVersion();
// 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=newDOMXPath($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)
包括符合標準支持HTML5文檔的符合標準支持的新DOM API,在DOM功能的行為中修復了幾個長期存在的合規性錯誤,並添加了多個功能,以使使用文檔更方便。
新的DOM API可在DOM名稱空間內使用。可以使用Domhtmldocument和DomxMldocument類創建使用新DOM API的文檔。
BCMATH的對象API
$num2='2';
$result=bcadd($num1,$num2,5);
echo$result;// '2.12345'
var_dump(bccomp($num1,$num2)>0);// false

$num1=newNumber('0.12345');
$num2=newNumber('2');
$result=$num1+$num2;
echo$result;// '2.12345'
var_dump($num1>$num2);// false
使用任意精度數字時,新的BCMATHNUMBER對象可以啟用面向對象的用法和標準數學運算符。
這些對像是不可變的,並實現了可弦樂接口,因此可以在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驅動程序特定子類
'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()沒有括號
public function getVersion(): string{
return'PHP 8.4';
}
}
var_dump(( newPhpVersion())->getVersion());

public function getVersion(): string{
return'PHP 8.4';
}
}
var_dump(newPhpVersion()->getVersion());
現在可以訪問新實例化對象的屬性和方法,而無需將新表達式包裹在括號中。
新增類別、介面和函數
- New Lazy Objects.
- New JIT implementation based on IR Framework.
- New request_parse_body() function.
- 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::getMicrosecond(), and DateTimeImmutable::setMicrosecond() methods.
- New mb_trim(), mb_ltrim(), mb_rtrim(), mb_ucfirst(), and mb_lcfirst() functions.
- New pcntl_getcpu(), pcntl_getcpuaffinity(), pcntl_getqos_class(), pcntl_setns(), and pcntl_waitid() functions.
- New ReflectionClassConstant::isDeprecated(), ReflectionGenerator::isClosed(), and ReflectionProperty::isDynamic() methods.
- New http_get_last_response_headers(), http_clear_last_response_headers(), and fpow() functions.
- New XMLReader::fromStream(), XMLReader::fromUri(), XMLReader::fromString(), XMLWriter::toStream(), XMLWriter::toUri(), and XMLWriter::toMemory() methods.
- New grapheme_str_split() function.
棄用和向後相容性中斷
- IMAP, OCI8, PDO_OCI, and pspell extensions have been unbundled and moved to PECL.
- Implicitly nullable parameter types are now deprecated.
- Using _ as a class name is now deprecated.
- Raising zero to the power of a negative number is now deprecated.
- Passing invalid mode to round() throws ValueError.
- Class constants from extensions date, intl, pdo, reflection, spl, sqlite, xmlreader are typed now.
- GMP class is now final.
- MYSQLI_SET_CHARSET_DIR, MYSQLI_STMT_ATTR_PREFETCH_ROWS, MYSQLI_CURSOR_TYPE_FOR_UPDATE, MYSQLI_CURSOR_TYPE_SCROLLABLE, and MYSQLI_TYPE_INTERVAL constants have been removed.
- mysqli_ping(), mysqli_kill(), mysqli_refresh() functions, mysqli::ping(), mysqli::kill(), mysqli::refresh() methods, and MYSQLI_REFRESH_* constants have been deprecated.
- stream_bucket_make_writeable() and stream_bucket_new() now return an instance of StreamBucket instead of stdClass.
- exit() behavioral change.
- E_STRICT constant has been deprecated.
有關PHP 8.4的源下載,請訪問下載頁。 Windows二進製文件可以在Windows的PHP地點。更改列表記錄在ChangElog。
這遷移指南可在PHP手冊中使用。請諮詢以獲取新功能和向後不兼容的更改的詳細列表。