What’s coming in PHP 8.2?

Edouard Courty
3 min readOct 6, 2022

--

PHP 8.2 is due to release on the 24th of November: let’s take a look at the interesting features this update will bring to the ecosystem!

New features:

  • Read-only classes

PHP 8.1 already implements readonly properties, meaning they can only be initialized once and become immutable after that.

PHP 8.2 brings read-only classes to the table.

Declaring a readonly class will make all its properties readonly.

  • Intersection types

PHP 8.1 had union types, PHP 8.2 comes with intersection types, meaning the variable fits within the two intersected types.
Here is an example:

$argument needs to fit both A and B types.
  • Dynamic properties deprecation

Changing the value of a public property using the -> arrow accessor is deprecated in PHP 8.2 and will throw an ErrorException in PHP 9.0.

To be able to continue with this logic, the __get and __set magic function must be implemented.

  • New Random extension

PHP 8.2 comes with a new Random extension that gives access to new, and fast random generation functions (generating an integer, randomizing bytes…)

Quick usage example for Random\Randomizer

The random generation engine can also be changed like that:

Choosing a different random engine base on environment variables

The available engines are the following ones:

Random\Engine\Mt19937
Random\Engine\PcgOneseq128XslRr64
Random\Engine\Xoshiro256StarStar
Random\Engine\Secure
  • New standalone types

PHP 8.2 makes true and false standalone types. Using them in a union type was possible in PHP 8.1, like in the implementation of the standard function file_get_contents() :

file_get_contents(/* … */): string|false

It’s now possible for example to use them as the only return type for a function:

public function alwaysFalse(): false
{
return false;
}

Not sure how this is going to be used in an actual case though…

But oh well, if needed it’s here!

  • Traits constants

Traits can now have const properties.
These properties can’t be accessed via the trait’s name, either from outside the trait or from inside it.

You can access the constant via a class that uses the trait, given that it’s public:

  • Sensitive parameters for safer backtraces

A common practice in any project is to send production errors to a service that keeps track of them and will notify developers when something goes wrong, usually using third-party services.

There are cases however where those stack traces can include sensitive information such as environment variables, passwords, or usernames.

PHP 8.2 allows you to mark these parameters as “sensitive” using an attribute, so they don’t get listed in your stack traces when something goes wrong.

Breaking changes:

  • New DateTime / DateTimeInterface return types

In PHP 8.1, the implementations of DateTime::createFromImmutable and DateTimeImutable::createFromMutable look like this:

DateTime::createFromImmutable(): DateTime DateTimeImmutable::createFromMutable(): DateTimeImmutable

Now in 8.2, the method signatures are these:

DateTime::createFromImmutable(): static DateTimeImmutable::createFromMutable(): static

This doesn’t seem like a big change, but it makes way more sense as it improves static insight possibilities for classes extending from DateTime and DateTimeImmutable.

However, technically, this is effectively a breaking change because it might affect any custom implementations that extend from either of those two classes.

  • Standard function deprecations

The two utf8_encode() and utf8_decode() methods are deprecated in PHP 8.2.

The RFC suggests using mb_convert_encoding() instead.

  • String manipulation functions changes

The two locale-insensitive strtolower() and strtoupper() function are not locale-sensitive any more. Localized conversion has to be done using mb_strtolower().

  • SPL methods signature changes

Several methods of SPL classes have been changed to properly enforce their correct type signature:

SplFileInfo::_bad_state_ex()
SplFileObject::getCsvControl()
SplFileObject::fflush()
SplFileObject::ftell()
SplFileObject::fgetc()
SplFileObject::fpassthru()
SplFileObject::hasChildren()
SplFileObject::getChildren()

That’s it for PHP 8.2!

This release only brings minor changes to the language and should not be too hard to migrate to, given the low number of breaking changes.

I mainly write about programming, cyber-security, and tech in

If you like my content and want to see more, follow my account to support me!

Have a good day :)

--

--

Edouard Courty

Web Developer & IT Teacher based in Paris - Back-end guru - Co-founder of @IMXrarity