PHP 8.2 brings type-system improvements for expressive and fine-grained type-safety, readonly classes, sensitive parameter redaction support, new random extension, and many new features along with several changes to streamline and modernize PHP language.
New Features 8.2
// Readonly Classes // Complete class properties become immutable when declared readonly readonlyclassUser{ publicfunction__construct(publicstring$username, publicstring$email) { } }
// Combine union and intersection types using Disjunctive Normal Form // 析取范式,允许通过 |(联合)和 &(交叉)运算符组合类型 functionprocess((Countable&Iterator)|null$item) { return$item; }
// Use null, true, false as independent types functionalwaysFalse(): false{} functionalwaysNull(): null{} functionalwaysTrue(): true{}
// Hide sensitive data in stack traces functionhashPassword(#[\SensitiveParameter]string$password) { debug_print_backtrace(); } hashPassword('secret123'); // Before #0 /home/user/scripts/code.php(7): hashPassword('secret123') // PHP8.2 #0 /home/user/scripts/code.php(7): hashPassword(Object(SensitiveParameterValue))
// Consolidated RNG functions with OOP interface $randomizer = new\Random\Randomizer(); echo$randomizer->getInt(1, 100); // Random integer
New Functions 8.2
// Parses any data size recognized by PHP INI values ini_parse_quantity('256M'); // 268435456
Deprecated Features 8.2
// ${var} String Interpolation Deprecated $name = 'Li'; echo"Hello ${name}"; // should be `{$name}` // Deprecated: Using ${var} in strings is deprecated, use {$var} instead $var = 'name'; echo"Hello ${$var}"; // should be `{$$var}` // Deprecated: Using ${expr} (variable variables) in strings is deprecated, use {${expr}}