Statement on glibc/iconv Vulnerability
Released!
PHP 8.2 は、PHP 言語のメジャーアップデートです。
このアップデートには、たくさんの新機能や最適化が含まれています。読み取り専用クラス、独立した型 null, false, true、動的なプロパティの非推奨化や、パフォーマンスの向上などが含まれています。

読み取り専用クラス RFC Doc

PHP < 8.2
class BlogData
{
public readonly
string $title;

public readonly
Status $status;

public function
__construct(string $title, Status $status)
{
$this->title = $title;
$this->status = $status;
}
}
PHP 8.2
readonly class BlogData
{
public
string $title;

public
Status $status;

public function
__construct(string $title, Status $status)
{
$this->title = $title;
$this->status = $status;
}
}

DNF(Disjunctive Normal Form)型 RFC Doc

PHP < 8.2
class Foo {
public function
bar(mixed $entity) {
if (((
$entity instanceof A) && ($entity instanceof B)) || ($entity === null)) {
return
$entity;
}

throw new
Exception('Invalid entity');
}
}
PHP 8.2
class Foo {
public function
bar((A&B)|null $entity) {
return
$entity;
}
}
DNF 型を使うと、union 型交差型 を組み合わせることができます。これらを組み合わせるときは、交差型は括弧で囲まなければいけません。

null, false, true が、独立した型に RFC RFC

PHP < 8.2
class Falsy
{
public function
almostFalse(): bool { /* ... */ *}

public function
almostTrue(): bool { /* ... */ *}

public function
almostNull(): string|null { /* ... */ *}
}
PHP 8.2
class Falsy
{
public function
alwaysFalse(): false { /* ... */ *}

public function
alwaysTrue(): true { /* ... */ *}

public function
alwaysNull(): null { /* ... */ *}
}

"Random" 拡張モジュール RFC RFC Doc

PHP 8.2
use Random\Engine\Xoshiro256StarStar;
use
Random\Randomizer;

$blueprintRng = new Xoshiro256StarStar(
hash('sha256', "Example seed that is converted to a 256 Bit string via SHA-256", true)
);

$fibers = [];
for (
$i = 0; $i < 8; $i++) {
$fiberRng = clone $blueprintRng;
// Xoshiro256**'s 'jump()' method moves the blueprint ahead 2**128 steps, as if calling
// 'generate()' 2**128 times, giving the Fiber 2**128 unique values without needing to reseed.
$blueprintRng->jump();

$fibers[] = new Fiber(function () use ($fiberRng, $i): void {
$randomizer = new Randomizer($fiberRng);

echo
"{$i}: " . $randomizer->getInt(0, 100), PHP_EOL;
});
}

// The randomizer will use a CSPRNG by default.
$randomizer = new Randomizer();

// Even though the fibers execute in a random order, they will print the same value
// each time, because each has its own unique instance of the RNG.
$fibers = $randomizer->shuffleArray($fibers);
foreach (
$fibers as $fiber) {
$fiber->start();
}

"random" 拡張モジュールは、乱数を生成するための、新しいオブジェクト指向の API を提供します。グローバルなシードに依存していた、メルセンヌ・ツイスターを使った乱数生成器(RNG) の代わりに、オブジェクト志向の API が複数の("エンジン" の)クラスを提供します。このクラスは、ステートをオブジェクトの内部に保存した状態で、モダンなアルゴリズムへのアクセスを提供します。これによって、複数の独立したシードのシーケンスを許容することができます。

\Random\Randomizer クラスは、エンジンのランダムな値を使って高レベルなインターフェイスを提供します。これを使うと、ランダムな数字を生成したり、配列や文字列をシャッフルしたり、配列のキーをランダムに選択したりなどができます。

トレイトで定数 RFC Doc

PHP 8.2
trait Foo
{
public const
CONSTANT = 1;
}

class
Bar
{
use
Foo;
}

var_dump(Bar::CONSTANT); // 1
var_dump(Foo::CONSTANT); // Error
トレイトの名前経由で定数にはアクセスできませんが、トレイトを使うクラスを通じて定数にアクセスできます。

動的なプロパティが非推奨に RFC Doc

PHP < 8.2
class User
{
public
$name;
}

$user = new User();
$user->last_name = 'Doe';

$user = new stdClass();
$user->last_name = 'Doe';
PHP 8.2
class User
{
public
$name;
}

$user = new User();
$user->last_name = 'Doe'; // Deprecated notice

$user = new stdClass();
$user->last_name = 'Doe'; // Still allowed

クラスを #[\AllowDynamicProperties] でマークしない限り、動的なプロパティの作成は推奨されなくなりました。これはミスや typo を防ぐのを助けるためです。stdClass は動的なプロパティを許可しています。

マジックメソッド __get/__set を使う場合は、この変更の影響を受けません。

新しいクラス、インターフェイス、関数

  • mysqli_execute_query, mysqli::execute_query
  • 新しいアトリビュート #[\AllowDynamicProperties],#[\SensitiveParameter]
  • ZipArchive::getStreamIndex, ZipArchive::getStreamName, ZipArchive::clearError
  • ReflectionFunction::isAnonymous, ReflectionMethod::hasPrototype
  • curl_upkeep, memory_reset_peak_usage, ini_parse_quantity, libxml_get_external_entity_loader, sodium_crypto_stream_xchacha20_xor_ic, openssl_cipher_key_length

非推奨および、非互換の変更

  • ${} 形式の、文字列への値の埋め込みは、推奨されなくなりました。
  • utf8_encodeutf8_decode は、推奨されなくなりました。
  • DateTime::createFromImmutableDateTimeImmutable::createFromMutable は、仮の戻り値の型が static になりました。
  • 拡張モジュール ODBCPDO_ODBC は、ユーザー名とパスワードをエスケープするようになりました。
  • strtolowerstrtoupper は、ロケールに依存しなくなりました。
  • SplFileObject::getCsvControl, SplFileObject::fflush, SplFileObject::ftell, SplFileObject::fgetc, SplFileObject::fpassthru は、シグネチャを強制するようになりました。
  • SplFileObject::hasChildren は、仮の戻り値の型が false になりました。
  • SplFileObject::getChildren は、仮の戻り値の型が null になりました。
  • 内部メソッド SplFileInfo::_bad_state_ex は、推奨されなくなりました。
To Top