Today is the official release of PHP 8.2.
And you can already use it on all your Platform.sh projects, with a single code change in your .platform.app.yaml
:
type: php:8.2
Try it out today!
PHP 8.2 introduces some pretty cool new features including:
- Readonly classes
- A new random number generator
- Disjunctive Normal Form Types
- Sensitive Parameter value redaction support
- Constants in Traits
- And the possibility to redact parameters in backtraces.
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;
}
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 {}
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');
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'
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 ‘.platform.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 today
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 .platform.app.yaml file in that branch and change the type key. After that, test your application and, if successful, merge the branch.
$ platform checkout main
$ platform branch upgrade-php-8.2
Change php version from .platform.app.yaml file from php:8.1 to php:8.2
# .platform.app.yaml
type: php:8.2
$ git add .platform.app.yaml && git commit -m "Upgrade PHP to v8.2"
$ platform deploy
Test your application
$ platform checkout main
$ platform 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 Platform.sh related topic on our public Slack channel or on our community website.
Happy Deploying!