What’s new in PHP 8.2

Tram Ho

PHP 8.2 will be released on November 24, 2022. In this post, we will explore the features, performance improvements, changes and deprecations in this release.

Readonly classes – Readonly classes rfc

In php 8.1 we have Readonly properties in 8.2 adding syntax to make all class properties read-only at once.

Functionally, making a class read-only is exactly the same as making every property read-only; but it will also prevent dynamic properties from being added to a class:

Note that you can only extend from readonly classes if the subclass is also a readonly class .

Deprecate dynamic properties – Don’t use dynamic properties rfc

I’d say this is a change for the better, but it will take a bit of a hit. Dynamic properties are deprecated in PHP 8.2 and will generate an ErrorException in PHP 9.0 :

New random extension – New random extension rfc

PHP 8.2 adds a new random number generator to fix many of the problems with the previous one: it’s more efficient, more secure, easier to maintain, and doesn’t rely on global state; eliminates a bunch of hard to spot errors when using PHP’s random functions.

There is a new class called Randomizer , which accepts a randomizer engine .

null , true , and false as standalone types rfc

PHP 8.2 adds three new types null, true and false that can be considered valid types . Common examples are PHP’s built-in functions, where false is used as the return type when an error occurs. Example in file_get_contents:

Before PHP 8.2, you could use false with other types as a conjugate; but now it can also be used as a standalone type:

Disjunctive Normal Form Types – DNF Types rfc

DNF types allow us to combine union (|) and intersection (&) types, following a strict rule: intersection types must be grouped with brackets. In fact, it looks like this:

In this case, (HasTitle & HasId) | null is of type DNF.

Constants in traits rfc

Now you can use constants in traits:

You won’t be able to access the constant through the name of the trait, from outside the trait, or from within it.

However, you can access the constant through the class using trait, provided it’s public:

Redact parameters in back traces rfc

When code running in production often has errors, we can use tracking services to detect this and send them to devs to fix. Usually when an error occurs, there will be stack traces to be able to detect the error, but it also comes with sensitive information such as environment variables, passwords or usernames.

PHP 8.2 allows you to mark such ” sensitive parameters ” with an attribute, so you don’t need to worry about them being listed in stack traces when something goes wrong. Here’s an example from the RFC:

Fetch properties of enums in const expressions – Get the value of enum in constant expression rfc

Don’t use string interpolation ${} rfc

PHP has several ways to embed variables in strings. This RFC does not accept two ways of doing so, as they are rarely used and often lead to confusion:

Just to be clear: two common ways of string interpolation still work:

Don’t use utf8_encode() and utf8_decode() rfc RFC suggests using mb_convert_encoding() instead

strtolower() and strtoupper() no longer support locale characters eg umlaut-a (ä) if you want to help use mb_strtolower()

See the beautiful version at: https://chungnguyen.xyz/posts/what-s-new-in-php-8-2

Share the news now

Source : Viblo