Did you know that PHP is used by over 20% of devs worldwide? If you’re one of them, you’re probably super eager to know more about the cool new features and improvements the latest release brings with it.
Released on 8th December 2022, PHP 8.2 marks the latest iteration of the PHP 8 line, building on the strong development of the language and adding syntactic sugar. There’s lots for devs to get excited about so we’re here to pull out some of the most interesting features and improvements added in PHP 8.2.
Ready?
So, what’s new in PHP 8.2?
The question every dev is asking, we've got the answer...
Readonly classes
Readonly properties were introduced in PHP 8.1 and this change builds on that, adding the ability to make all class properties readonly at once.
You go from this:
To this:
Making a class readonly is entirely the same as making each property readonly, but it will also prevent dynamic properties being added to a class.
Deprecated dynamic properties
Dynamic properties are deprecated in PHP 8.2 and will throw an ErrorException in PHP 9.0:
But remember that using __get and __set within the class will still work.
Random Extension
A new random number generator has been added! It's more efficient and secure, easier to maintain, and doesn’t rely on global state, eliminating a range of potential bugs.
The new class is called Randomizer, which accepts a randomizer engine. You can change that engine, depending on your needs.
Standalone types for null, true and false
Avoiding the type safety wars… PHP 8.2 adds three new types and null, true, and false could be considered valid types on their own. PHP's built-in functions often use these as the return type when an error occurs.
Disjunctive Normal Form Types
Quite a mouthful of a name, DNFs (as they're referred to) allow you to combine union and intersection types. You’ll need to remember that when combining union and intersection types, intersection types must be grouped with brackets. In the following example (HasTitle&HasId)|null is the DNF type.
Constants in traits
If you ever need to do this, constants can now be used within traits:
You won't be able to access the constant via the trait's name, either from outside the trait, or from inside it, but you can access the constant via the class that uses the trait:
What else is new in PHP 8.2?
There's a whole lot more included in the PHP 8.2 release (too much to cover in one post!). So, here’s a quick round-up of the RFCs, deprecations and breaking changes:
- Redact parameters in back traces
- Fetch properties of enums in const expressions
- Return type changes for DateTime::createFromImmutable() and DateTimeImmutable::createFromMutable()
- utf8_encode() and utf8_decode() deprecations
- Locale-insensitive strtolower() and strtoupper()
- Signature changes to several SPL methods
- New n modifier in PCRE
- ODBC username and password escaping
- Deprecate ${} string interpolation
- Deprecate partially supported callables
Migrating to PHP 8.2
If you’re looking to update your code, PHP have their own guide on migrating from PHP 8.1 to 8.2. Our Web Hosting and WordPress Hosting packages contain the latest version of PHP when they become available and we have our own list of tips and best practices for migrating to PHP 8!
If you're a Fasthosts customer, we also have other helpful guides on upgrading your packages to use the latest PHP version for Web Hosting.
Updating your WordPress site to PHP 8.2
Using WordPress? You should always check with your provider but many will support the latest PHP version when it's released (like us). Many providers will also have their own guides to show you how to migrate PHP version within their control panels – we have our own guide on changing PHP version for our WordPress customers. We also suggest that you make sure your themes and plugins are updated as well – you can set these to auto update so you don't have to do this manually.
Well that about sums it up. There's lots to unpack with the PHP 8.2 release and we've tried to cover most of it. If you have any questions about PHP and our Web Hosting, WordPress Hosting or an existing package, our expert support team are here 24/7 to lend a hand.