New Features, Improvements, and Deprecations in PHP 8.2
PHP 8.2 introduces extensive type‑system enhancements, readonly classes, a new random extension, trait constants, sensitive‑parameter support, several new functions, and deprecates dynamic properties and legacy string syntax, providing developers with modernized, safer backend capabilities.
PHP 8.2 has been officially released, bringing a range of modernizations to the language and its ecosystem.
Type System Improvements
Independent true , null and false types are now supported, and Disjoint Normal Form (DNF) types enable combining union and intersection types for more precise type declarations.
Disjoint Normal Form (DNF) type support – in PHP 8.2 developers can combine union types (PHP 8.0) and intersection types (PHP 8.1) to declare more exact parameter, return and property types.
<code>function process((HTMLRequest & RequestInterface) | APIRequest $request) {
// ...
}
</code>Adding true and false as independent types also allows changing a bool return type to a more specific literal type.
<code>function alwaysReturnsFalse(): false {}
function alwaysReturnsNull(): null {}
function alwaysReturnsTrue(): true {}
</code>Readonly Classes
Readonly properties are extended to entire classes; declaring a class as readonly makes all its properties readonly and enforces type declarations.
<code>// PHP 8.2
readonly class User {
public string $username;
public int $uid;
}
// PHP 8.1 equivalent
class User {
public readonly string $username;
public readonly int $uid;
}
</code>New Random Extension
The new random extension consolidates all RNG‑related functions and provides an object‑oriented API while keeping existing functions ( rand , mt_rand , random_bytes , random_int ) fully compatible.
<code>use Random\Engine\Secure;
$engine = new Secure();
$rand = new Random\Randomizer($engine);
$value = $rand->getInt(0, 100);
</code>Trait Constants
Traits can now define constants. Although a trait’s constants cannot be accessed directly, they become class constants when the trait is used.
<code>trait FooBar {
const FOO = 'foo';
private const BAR = 'bar';
final const BAZ = 'baz';
final protected const QUX = 'qux';
}
class Test {
use FooBar;
}
echo Test::BAZ; // 'baz'
</code>Sensitive Parameter Support
PHP 8.2 adds the #[\SensitiveParameter] attribute, which hides the actual value of annotated parameters in error and stack traces, replacing them with a SensitiveParameterValue object.
<code>function passwordHash(#[\SensitiveParameter] string $password) {
debug_print_backtrace();
}
passwordHash('hunter2');
</code> <code>array(1) {
[0]=> object(SensitiveParameterValue)#1 (0) {}
}
</code>New Functions and Classes
ini_parse_quantity() – parses INI size strings (e.g., '256M' → 268435456 ).
curl_upkeep() – triggers internal Curl tasks to keep connections alive.
openssl_cipher_key_length() – returns the required key length for a given OpenSSL cipher.
memory_reset_peak_usage() – resets the peak memory usage reported by memory_get_peak_usage() .
Deprecations in PHP 8.2
Several legacy features are deprecated:
Dynamic properties – creating undeclared class properties now triggers a deprecation notice.
utf8_encode and utf8_decode – these functions actually operate on Latin‑1 and are removed.
String interpolation using ${var} – the syntax with a dollar sign outside braces is deprecated.
Partial support for callable patterns and certain Mbstring Base64/Uuencode/QPrint/HTML entity handling.
<code>class User {
public int $uid;
}
$user = new User();
$user->name = 'Foo'; // Deprecated: Creation of dynamic property User::$name
</code>Developers can silence these deprecations by using #[AllowDynamicProperties] , magic __get/__set methods, or by explicitly allowing dynamic properties.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.