What is New in PHP 8.2
PHP 8.2 delivers many modern features and performance improvements. The highlight of this release is the introduction of readonly classes, the new Random extension, standalone types, and several quality-of-life enhancements that make code cleaner and safer.
Key New Features
Readonly Classes
A class can now be marked as readonly. When a class is readonly, all its properties automatically become readonly as well.
readonly class User
{
public function __construct(
public string $name,
public string $email
) {}
}
// $user->name = 'new name'; // Fatal error
Disjunctive Normal Form (DNF) Types
PHP now supports complex union and intersection types using DNF syntax.
function process((A&B)|(C&D) $param): (X&Y)|(Z) { ... }
New Standalone Types: true, null, false
true, null, and false can now be used as standalone types.
function alwaysTrue(): true { return true; }
function redirect(): never { exit; }
New Random Extension
A completely new Random extension provides a modern, object-oriented, and cryptographically secure way to generate random values.
$rng = new \Random\Engine\Secure();
$randomizer = new \Random\Randomizer($rng);
echo $randomizer->getInt(1, 100);
Allow constants in Traits
Traits can now declare constants.
trait ApiSettings {
public const API_VERSION = 'v2';
}
Deprecate Dynamic Properties
Creating dynamic properties on objects (except stdClass) is now deprecated and will become an ErrorException in PHP 9.0.
$obj = new stdClass;
$obj->dynamic = 'value'; // Still allowed
class User {}
$user = new User;
$user->name = 'John'; // Deprecated in 8.2
New curl_upkeep() Function
Perform upkeep tasks on a cURL connection handle (useful for long-lived connections.
New memory_reset_peak_usage() Function
Resets the peak memory usage counter back to the current value.
New Functions and Classes
| Area | New Items |
|---|---|
| Random Extension | Random\Randomizer, Random\Engine\Secure, Random\Engine\Mt19937, random_int(), random_bytes() improvements |
| cURL | curl_upkeep() |
| Memory | memory_reset_peak_usage() |
| Intl | ini_get_all() now supports details parameter for extensions |
| Password Hash | password_needs_rehash() supports $options argument |
| SPL | SplFileObject::getSize() |
Notable Deprecations
- Creating dynamic properties on classes (except
stdClass) is deprecated. ${}string interpolation syntax is deprecated.utf8_encode()andutf8_decode()are deprecated - usemb_convert_encoding()or iconv instead.- Calling
get_class()andget_parent_class()without arguments is deprecated. mbstring.func_overloadis fully removed.create_function()has been completely removed.
Performance Improvements
PHP 8.2 brings many internal optimizations:
- Faster property access and method calls.
- Improved handling of readonly classes and properties.
- Better JIT performance and memory usage.
- Reduced overhead for heredoc/nowdoc and flexible heredoc indentation.
- Overall lower memory footprint for many common operations.
Other Changes
localeconv()now returns decimal and thousands separators as strings ornull.fopen()with"w+"mode on non-existent directories no longer triggers a warning.array_is_list()function added to check if an array has sequential numeric keys starting from 0.- New
sensitive parameterattribute to hide values in stack traces.