What’s new in PHP 8.3

TL;DR: PHP 8.3 will be released on November 23, 2023. It will add typed class constants, a new helper function json_validate to check json payloads and some minor improvements for Randomizer class, reading ini configuration and more.

PHP Elephants Migrating PHP8.3

The PHP team will release the new minor version of PHP to the end of this month. The new version will contain mostly improvements and features not very relevant for end users, except than a few. I briefly want to introduce them here as part of my PHP blog series.

Typed Class Constants

We all use constants at some point. So far, the type of constants were inferred by their value. This also means, inheriting or implementing classes were able to change the type. In short, the new feature will look like:

Validating JSON payloads

In almost every scenario where JSON has to be read, transformed and written, it has to be validated first. Either by doing a “test decode” to see whether an exception is thrown or just having an else-Branch for the invalid payload case.

This is probably ok with smaller payloads but decoding very large JSON strings to arrays to only check whether it is valid is not elegant. Further, it can cause memory and/or performance issues.

The new json_validate function promises to check whether a given string is a valid JSON and is much more performant and memory saving. The whole signature looks like:

Further Improvements

As I said, there are further improvements for the upcoming 8.3 version of PHP. As I personally think they are not so important, I just want to mention them here for the sake of completeness.

  • Randomizer Improvements: Small improvements, like giving a range for random float values or defining string lengths.
  • Readonly amendments: allows readonly properties to get reinitialized while cloning. This seems like an edge case, but can be important for deep cloning.
  • #[Override] attribute: this attribute is utilized to indicate a programmer’s “intent”. It essentially expresses “I know this method comes from a parent class and I want to override it. Will this ever change, please let me know.”
  • Anonymous readonly classes: somehow self explanatory. Until know, it was not possible to create them, now PHP introduces it.
  • Dynamic class constant fetch: PHP allows reading dynamic class properties using curly brackets. With 8.3, it is also possible to read constants (seriously, do not do it).
  • Static properties in traits: will redeclare static properties inherited from the parent class. Personally, this is the mix of all evil you can do in PHP. Why? Check this.

And many more. Since I personally think they are not relevant, I did not list them here. Check the official migration guide for a complete list of all changes.

Conclusion

Interesting times ahead. As with every new version, there is no golden rule to follow whether to update or not. For my repositories (e.g. Keestash), we will definetely go for new(est) version(s).

However, upgrading should be well planned and realized. Having a solid test coverage and a good process of individual and automated testing can help reducing risk and upgrading a success.

You want to upgrade your code base, but do not how? Let’s talk 🙂