PHP is one of the most widely used server-side scripting languages. The updates are quite frequent maintaining remarkable performance and security of PHP.
Since, PHP version 8.2 is going to release on November 24, 2022, we will go through the changes, performance enhancements, and depreciations in PHP 8.2.
PHP 8.2 has upgraded the random number generator function fixing a lot of problems in the previous version. It is now more secure, easier to implement, and does not depends on the global state.
You can now pass your desired randomizer engine to the new Randomizer class. For instance, to differentiate between testing and production environment.
$engine = $is_production |
With the version 8.2, we will have three new standalone types in PHP.
|
Let’s take an example of a built-in function file_get_contents where false is used as the return type.
file_get_contents(/* ... */): string|false |
Technically, these are valid types on their own, but now you can use false as a standalone type as well.
function falseAlways(): false |
DNF is a nice addition in PHP 8.2 as it allows us to combine intersection and union types. It provides nullable intersection types, which are likely to be the most important use case in the future.
Following a strict rule, while combining intersection and union types, the intersection types must be grouped using brackets. For example, in the following use case (HasTitle&HasId)|null is the Disjunctive Normal Form Type.
function generateMeta((HasTitle&HasId)|null $blog) |
Both strtoupper() and strtolower() functions are now locale-insensitive. You can use mb_strtoupper() and mb_strtolower() for localized case conversion.
mb_strtoupper(string $string, ?string $encoding = null): string |
mb_strtolower(string $string, ?string $encoding = null): string |
PHP 8.2 allows you to use constants in traits. You will not be able to access the constant through the trait’s name, either from inside the trait or outside of it. However, you can access the (public) constant through the class that uses the trait.
trait Test |
class Bar |
Now, you can use -> / ?-> to fetch the properties of enums in constant expressions.
The primary reason to allow this is to fetch the value and name properties in places where enum objects are not allowed, just like the array keys.
enum A: string |
PHP provides several ways of using variables in strings. This upgrade deprecates the following two ways of string interpolation as they are not commonly used and sometimes lead to confusion. Deprecated:
"Hello ${PHP}"; |
While the following string interpolation methods still work.
"Hello {$PHP}"; "Hello $PHP"; |
When both connection string and username/password are passed to the ODBC extension, it will escape the username and password and the string must be appended.
This same change applies to the PDO_ODBC
The readonly property feature was introduced in PHP 8.1. The latest version will use this property to make all properties of a class readonly at once. For example:
readonly class Blog |
That's all for now. We have highlighted the most important new features and changes in PHP. You can adapt these to improve your code quality. Happy Coding.