• 0030 2107517586
  • info@wizdom.gr

What's new in PHP 8.2

What's new in PHP 8.2

PHP is one of the most widely used server-side scripting languages. The updates are quite frequent maintaining remarkable performance and security of PHP. 


Since, PHP version 8.2 is going to release on November 24, 2022, we will go through the changes, performance enhancements, and depreciations in PHP 8.2.

New random extension

PHP 8.2 has upgraded the random number generator function fixing a lot of problems in the previous version. It is now more secure, easier to implement, and does not depends on the global state.


You can now pass your desired randomizer engine to the new Randomizer class. For instance, to differentiate between testing and production environment.


$engine = $is_production
    ? new Random\Engine\Secure()
    : new Random\Engine\Mt19937(4321);

$randomizer = new Random\Randomizer($engine);


New standalone types in PHP 8.2

With the version 8.2, we will have three new standalone types in PHP. 

  1. null
  2. true
  3. false


Let’s take an example of a built-in function file_get_contents where false is used as the return type.


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


Technically, these are valid types on their own, but now you can use false as a standalone type as well.


function falseAlways(): false
    return false;


Disjunctive Normal Form Types

DNF is a nice addition in PHP 8.2 as it allows us to combine intersection and union types. It provides nullable intersection types, which are likely to be the most important use case in the future.


Following a strict rule, while combining intersection and union types, the intersection types must be grouped using brackets. For example, in the following use case (HasTitle&HasId)|null is the Disjunctive Normal Form Type.


function generateMeta((HasTitle&HasId)|null $blog)
    if ($blog === null) {
        return '';

        . $blog->getId();


Modification in strtolower() and strtoupper() functions

Both strtoupper() and strtolower() functions are now locale-insensitive. You can use mb_strtoupper() and mb_strtolower() for localized case conversion.


mb_strtoupper(string $string, ?string $encoding = null): string


mb_strtolower(string $string, ?string $encoding = null): string


Constants in traits

PHP 8.2 allows you to use constants in traits. You will not be able to access the constant through the trait’s name, either from inside the trait or outside of it. However, you can access the  (public) constant through the class that uses the trait.


trait Test
    public const CONSTANT = 9;

    public function foo(): int
        return self::CONSTANT;

class Bar
    use Test;

Bar::CONSTANT; // 9


Fetch properties of enums in const expressions

Now, you can use -> / ?-> to fetch the properties of enums in constant expressions. 


The primary reason to allow this is to fetch the value and name properties in places where enum objects are not allowed, just like the array keys.


enum A: string
    case B = 'B';
    const C = [self::B->value => self::B];


${} string interpolation deprecation

PHP provides several ways of using variables in strings. This upgrade deprecates the following two ways of string interpolation as they are not commonly used and sometimes lead to confusion. Deprecated: 


  1. Using ${} in strings is deprecated
  2. Using ${} (variable variables) in strings is deprecated


"Hello ${PHP}";

"Hello ${(PHP)}";


While the following string interpolation methods still work.


"Hello {$PHP}";

"Hello $PHP";


ODBC username and password escaping

When both connection string and username/password are passed to the ODBC extension, it will escape the username and password and the string must be appended.


This same change applies to the PDO_ODBC


Readonly classes

The readonly property feature was introduced in PHP 8.1. The latest version will use this property to make all properties of a class readonly at once. For example:

readonly class Blog
    public function __construct(
        public string $title,
        public string $body,
        public DateTime $publishedAt,
    ) {}

That's all for now. We have highlighted the most important new features and changes in PHP. You can adapt these to improve your code quality. Happy Coding.