

PHP 8.4は、PHP言語(yǔ)の主要な更新です。
プロパティフック、非対稱(chēng)の可視性、更新されたDOM API、パフォーマンスの改善、バグ修正、一般的なクリーンアップなど、多くの新機(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
プロパティフックは、同期が外れる可能性のあるDocblockコメントを書(shū)く必要なく、IDESおよび靜的分析ツールによってネイティブに理解できる計(jì)算プロパティをサポートします。さらに、クラスに一致するゲッターまたはセッターが存在するかどうかを確認(rèn)することなく、値の信頼できる前処理または後処理を許可します。
非対稱(chēng)可視性
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}" ;
}
}
プロパティに書(shū)き込む範(fàn)囲は、プロパティを読むために範(fàn)囲から獨(dú)立して制御されるようになり、クラスの外側(cè)からの変更を許可することなく、ボイラープレートゲッターメソッドがプロパティの価値を公開(kāi)する必要性を減らします。
#[\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機(jī)能と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)
HTML5ドキュメントの解析のための標(biāo)準(zhǔn)に準(zhǔn)拠したサポートを含む新しいDOM API、DOM機(jī)能の動(dòng)作におけるいくつかの長(zhǎng)年のコンプライアンスバグを修正し、ドキュメントをより便利にするためのいくつかの機(jī)能を追加します。
新しいDOM APIは、DOM Namespace內(nèi)で使用できます。新しいDom APIを使用したドキュメントは、domhtmldocumentおよびdomxmldocumentクラスを使用して作成できます。
bcmathのオブジェクト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
任意の精度番號(hào)を使用する際に、新しいbcmathnumberオブジェクトは、オブジェクト指向の使用法と標(biāo)準(zhǔn)の數(shù)學(xué)演算子を有効にします。
これらのオブジェクトは不変であり、弦楽器のインターフェイスを?qū)g裝するため、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"
new Functions 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、およびPDOSQLITEのPDOが利用可能です。
new MyClass()->method()括弧なし
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());
新たにインスタンス化されたオブジェクトのプロパティと方法に、括弧內(nèi)の新しい式を包むことなくアクセスできるようになりました。
新しいクラス、インターフェイス、関數(shù)
- 新しい怠zyなオブジェクト。
- IRフレームワークに基づく新しいJIT実裝。
- new 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 :: GetMicRoSecond()、およびDateTimeimmutable :: SetMicRoseCond()
- 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ù)。
- new ReflectionClassConstant :: isDeprecated()、ReflectionGenerator :: ISClosed()、およびReflectionProperty :: isDynamic()メソッド。
- new http_get_last_response_headers()、http_clear_last_response_headers()、およびfpow()関數(shù)。
- new xmlreader :: fromstream()、xmlreader :: fromuri()、xmlreader :: fromstring()、xmlwriter :: toStream()、xmlwriter :: touri()、およびxmlwriter :: tomemory()メソッド。
- new Grapheme_str_split()関數(shù)。
非推奨と下位互換性の中斷
- IMAP, OCI8, PDO_OCI, and pspell extensions have been unbundled and moved to PECL.
- 暗黙のうちに無(wú)視可能なパラメータータイプが非推奨になりました。
- _をクラス名として使用すると、これが非推奨になります。
- 負(fù)の數(shù)のパワーにゼロを上げることは、今では非推奨になります。
- 無(wú)効なモードをRound()に渡すと、ValueErrorをスローします。
- 拡張日、INTL、PDO、リフレクション、SPL、SQLITE、XMLReaderのクラス定數(shù)が現(xiàn)在入力されています。
- GMPクラスが最終的になりました。
- 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()functions、mysqli :: ping()、mysqli :: kill()、mysqli :: refresh()メソッド、およびmysqli_refresh_*定數(shù)は非推奨です。
- stream_bucket_make_writable()およびstream_bucket_new()は、stdclassの代わりにstreambucketのインスタンスを返すようになりました。
- exit()行動(dòng)の変化。
- e_strict定數(shù)は非推奨です。
PHP 8.4のソースダウンロードについては、をご覧ください ダウンロード ページ。 Windowsバイナリはにあります Windows用のPHP サイト。変更のリストはに記録されます Changelog。
The 移行ガイド PHPマニュアルで入手できます。新機(jī)能と後方に取得できない変更の詳細(xì)なリストについては、それを參照してください。