Save up to $19 on app hosting. Every month. Find out more

PHP 8.2 features and changes

PHPimprovementsfeatures
31 December 2022
Rémi Lejeune
Rémi Lejeune
Product Manager
Share

Today is the official release of PHP 8.2.

And you can already use it on all your Upsun projects, with a single code change in your .upsun.app.yaml

type: php:8.2

Try it out today!

PHP 8.2 introduces some pretty cool new features including: 

Here’s a quick breakdown of each new feature and how they ought to make things easier for you. 

Readonly classes

Previously, PHP 8.1 added support for readonly class properties. Now, the new PHP 8.2 supports declaring an entire class as readonly.

Readonly classes are declared with the readonly keyword before the class declaration:

readonly class MyValueObject {
    public string $myValue;
}

Copy the snippet

Abstract classes and final classes can also be declared readonly. The order of the keywords does not make a difference, which makes things fairly easy.

abstract readonly class Foo {}
final readonly class Bar {}

Copy the snippet

It’s also possible to declare a readonly class with no properties in them, which effectively prevents dynamic properties while allowing child classes to explicitly declare their readonly properties.

New random number extension

PHP 8.2 introduces a new PHP extension named random, which organizes and consolidates existing PHP functionality related to random number generation.

It also introduces a series of PHP class structures and exception classes to provide granular choice of random-number generator and exception handling.

Disjunctive Normal Form (DNF) types

Another new PHP 8.2 feature is Disjunctive Normal Form (DNF), which is a standard way of organizing boolean expressions. 

Specifically, that means you can structure a boolean expression into an ORed series of ANDs. When applied to type declarations, this allows for a standard way to write combined Union and Intersection types that the parser can handle.

Sensitive Parameter value redaction support

In PHP 8.2, it is now possible to mark sensitive parameters with a PHP attribute named SensitiveParameter, which makes PHP redact the sensitive information from the stack trace.

function passwordHash(#[\SensitiveParameter] string $password) {
    var_dump(debug_backtrace());
}

passwordHash('hunter2');

Copy the snippet

Constants are supported in traits

Declaring constants in traits is now supported, too. Trait constants can also be declared with visibility modifiers and as final (since PHP 8.1). 

All of the following constant declarations are valid in PHP 8.2 and later:

trait FooBar {
    const FOO = 'foo';
    private const BAR = 'bar';
    final const BAZ = 'baz';
    final protected const QUX = 'qux';
}

class Test {
    use FooBar;
}

echo Test::BAZ; // 'baz'

Copy the snippet

With PHP 8.2, there are now a few deprecations…

PHP 8.2 does introduce some deprecations, such as the Dynamic Properties, the ‘${}’ string interpolation, the partially supported callables, and the ‘utf8_encode’ and  'utf8_decode' methods.

End of ZTS support

Zend Thread Safe (ZTS) has been supported by our PHP images since the 7.1 version. 

But with a diminution of the usage, and extensions dropping the support, the 8.2 version of our PHP image, and future versions, do not support ZTS.

Extensions

Some PHP extensions might not be available on PHP 8.2. While extensions are already available, you may need to wait for others to arrive, such as newrelic, ioncube, sourceguardian, event, interbase, mongodb, oauth, pdo_sqlsrv, tideways, and xdebug.

They will roll out incrementally, and once they are available the documentation will be updated accordingly. All you need to do is to update your .upsun.app.yaml file to enable them and push the new configuration.

Composer 2

Composer, the PHP dependency manager has been updated to Composer 2. 

But don’t worry, Composer 2 will be mostly compatible with your existing workflows, and it brings with it  great new features, including faster download times and run-time platform requirements checks.

Blackfire support for PHP 8.2

Meanwhile, PHP 8.2 will also work with Blackfire starting on its release date on December 8, 2022! 

You can find out more about PHP 8.2 with Blackfire on our blog.

There you have it! Try it out

As you may figure, PHP 8.2 does introduce some breaking changes that may affect older code. So, as a best practice, you will want to test your application in a development environment.

To do that, first, create a new branch in your repository. Then, update your ./.upsun/config.yaml in that branch and change the type key. After that, test your application and, if successful, merge the branch.

$ upsun checkout main
$ upsun branch upgrade-php-8.2

Change php version in ./.upsun/config.yaml file from php:8.1 to php:8.2

# ./.upsun/config.yaml
type: php:8.2

$ git add ./.upsun/config.yaml && git commit -m "Upgrade PHP to v8.2"
$ upsun deploy

Test your application

$ upsun checkout main
$ upsun merge upgrade-php-8.2

Congratulations! You’re now running PHP 8.2 on production.

And if you encounter or simply want to suggest new or improved features, please tell us on our GitHub repository.

You can also start a conversation on any Upsun.com related topic on our community website.

Happy Deploying!

Useful links: