PHP 8.2 Comes with Functional and Performance Improvements

PHP 8.2 Comes with Functional and Performance Improvements

PHP 8.2 was released, bringing read-only classes, new standalone types, a new random extension, and deprecated dynamic properties.

Regarding web applications, PHP is one of the greatest programming languages. It is a dynamic, general-purpose scripting language that is used in the development of server-side applications.

Leading CMS platforms such as WordPress, Joomla, Drupal, Magento, and others, which provide the majority of web content on the Internet, are built using PHP. That is why each new version generates great attention and response among web developers.

Although minor, the recently released PHP 8.2 version introduces some significant changes to PHP’s journey toward its modernization.

What’s New in PHP 8.2 Scripting Language

One year after the release of the previous PHP 8.1 version, PHP 8.2 is here with another dose of new features. So let’s look at them.

Readonly Classes

PHP 8.1 introduced the readonly properties. With readonly classes, PHP 8.2 takes readonly properties much further. Previously, you could create a class and define all the properties as readonly. So far, so good, but doing this can be time-consuming.

PHP 8.2 introduced a significantly better solution to this problem – readonly classes. So, when you declare a class as readonly, all properties within it are readonly from that point on.

For example, instead of writing this:

class LinuxDistros
{
    public function __construct(
        public readonly string $name, 
        public readonly string $package_manager,
        public readonly DateTimeImmutable $last_release_date,
    ) {}
}

You can now write the following:

readonly class LinuxDistros
{
    public function __construct(
        public string $name, 
        public string $package_manager,
        DateTimeImmutable $last_release_date,
    ) {}
}

It is important to note that you can only extend from readonly classes provided the child class is also readonly.

New Standalone Types

Before PHP 8.2, it was only possible to use null, false, and true as parts of union types. So, if you tried declaring them as standalone types, it resulted in a fatal error. However, PHP 8.2 now supports using null, false, and true as standalone types.

function returnFalse(): false
{
    return false;
}

The same logic and applicability also apply to true and null. In other words, precisely declaring the return, parameter, and property types make PHP’s type system more expressive and complete.

New Random Extension

Historically, PHP has always supported a variety of Random Number Generators (RNGs) with varying degrees of performance, use cases, and appropriateness for secure applications.

PHP 8.2 introduces a new random number generator that addresses many issues with the previous one: it is more performant and secure. Randomizer is a new class that accepts a randomizer engine.

use Random\Randomizer;
$randomizer = new Randomizer();
$randomizer->shuffleBytes('BTW I Use Arch');

Of course, you’re not limited to strings. You can also randomize arrays, numbers, and so on.

$randomizer->shuffleBytes(['a', 'b', 'c']);
$randomizer->getInt(0, 100);

Furthermore, depending on your requirements, you can change this engine.

Constants in Traits

A trait in PHP is a mechanism that allows developers to reuse methods from independent classes that exist in various inheritance hierarchies. Traits currently only allow the definition of methods and properties, but not constants.

However, in PHP 8.2, you can now use constants in traits.

trait Linux 
{
    public const CONSTANT = 1;
 
    public function distro(): int 
    {
        return self::CONSTANT;
    }
}

Other PHP 8.2 Highlights

The union and intersection types improved the language greatly. But PHP 8.2 adds even more to that. For example, the new disjunctive normal form (DNF) types allow you to combine union and intersection types while following a strict rule: intersection types must be grouped with brackets when combining union and intersection types.

PHP 8.2 also includes various deprecations. For example, ${} string interpolation and the utf8_encode and utf8_decode functions are now deprecated. So, when used, PHP will produce a deprecation notification, which should not interfere with adequately configured PHP applications but will be logged into the error log.

You can refer to the official announcement for detailed information about all changes in PHP 8.2.

Bobby Borisov
Bobby Borisov

Bobby is an Editor-in-Chief at Linuxiac. He is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, Bobby has worked as a Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Leave a Reply

Your email address will not be published. Required fields are marked *