PHP 8.2 is now available and comes with read-only properties of classes and other new features

PHP 8.2 is now available and comes with read-only properties of classes and other new features

Let's start by exploring all the latest features of PHP 8.2. It's quite a long list:

New readonly classes
Allow true, false, and null as standalone types
Types of Disjunctive Normal Form (DNF)
Redact sensitive parameters in Back Traces
New mysqli_execute_query function and mysqli::execute_query method
Retrieve enum properties in const expressions
Allow Constants in Traits
Deprecation of dynamic properties (and new #[AllowDynamicProperties] attribute)
Deprecation of partially supported callers
Deprecation of #utf8_encode() and utf8_decode() functions
${} deprecation String interpolation
Deprecation of mbstring functions for Base64/QPrint/Uuencode/HTML entities
Removed libmysql support from mysqli
Conversion of location-independent cases
Improved Random Extension

New readonly classes

PHP 8.1 introduced readonly functionality for class properties. Now PHP 8.2 adds support for declaring the entire class as readonly.

If you declare a class as readonly, all of its properties will automatically inherit readonly functionality. Thus, declaring a class readonly is equivalent to declaring each class property as readonly.

You can also declare a readonly class with no properties. Effectively, this prevents dynamic properties while allowing child classes to explicitly declare their readonly properties.

Allow true, false, and null as standalone types

PHP already includes scalar types like int, string, and bool. This was extended in PHP 8.0 with the addition of union types, allowing values to be of different types. The same RFC also allows the use of false and null as part of a union type – they are not allowed as standalone types, however.

If you try to declare false or null as standalone types – without them being part of a union type, then you'll get a fatal error.

To avoid this scenario, PHP 8.2 adds the ability to use false and null as standalone types. With this addition, PHP's type system is more expressive and complete. You can now precisely declare return, parameter, and property types.

Also, PHP still doesn't include a true type, which seems to be the natural counterpart of the false type. PHP 8.2 fixes this and also adds support for the true type. It does not allow coercion, just like the false type.

The two types true and false are basically a union type of PHP's bool type. To avoid redundancy, you cannot declare these three types together in a union type. This would result in a fatal compile error.

Types of Disjunctive Normal Form (DNF)

Disjunctive Normal Form (DNF) is a standardized way to organize Boolean expressions. It consists of a disjunction of conjunctions – in Boolean terms, it is an OR of AND.

Applying the DNF to type declarations makes it possible to write in a standard way combined Union and Intersection types that the parser can handle. PHP 8.2's new DNF types feature is simple yet powerful if used correctly.

New mysqli_execute_query function and mysqli::execute_query method

PHP 8.2 makes it easier to execute parameterized MySQLi queries with the new mysqli_execute_query($sql, $params) function and mysqli::execute_query method.

Essentially, this new function is a combination of the mysqli_prepare(), mysqli_execute(), and mysqli_stmt_get_result() functions. With it, the MySQLi query will be prepared, bound (if you pass parameters) and executed within the function itself. If the query runs successfully, it returns a mysqli_result object. If unsuccessful, it will return false.

Retrieve enum properties in const expressions

The main reason for this new feature is that you cannot use enum objects in certain places, such as array keys. In such a case, you will have to repeat the value of the case enum just to use it.

Allowing retrieval of enum properties in places where enum objects are not allowed can simplify this procedure.

Deprecation of mbstring functions for Base64/QPrint/Uuencode/HTML entities

PHP's mbstring (multi-byte character string) functions help us work with Unicode, HTML entities, and other older text encodings.

However, Base64, Uuencode, and QPrint are not text encodings and are still among these functions - mainly for legacy reasons. PHP also includes separate implementations of these encodings.

As for HTML entities, PHP has built-in functions – htmlspecialchars() and htmlentities() – to handle them better. For example, unlike mbstring, these functions will also convert <. > and & to HTML entities.

Moreover, PHP is constantly improving its built-in functions – like PHP 8.1 with HTML encoding and decoding functions.

So, with all of this in mind, PHP 8.2 deprecates the use of mbstring for these encodings (labels are case insensitive):

BASE64
UUENCODE
HTML-ENTITIES
html (aka HTML-ENTITIES)
Quoted-Printable
qprint (aka Quoted-Printable)
As of PHP 8.2, using mbstring to encode/decode any of the above will issue a deprecation notice. PHP 9.0 will completely remove mbstring support for these encodings.