new features in php 8.2

PHP 8.2 announced - new and deprecated features

7 min read

With PHP being one of the most widely utilized web programming languages to date, it isn’t surprising that new and improved features are being released on a regular basis.

The latest PHP update - 8.2, is expected to go live in November 2022 and it promises to introduce some new, valuable features! This year’s update promises to implement readonly classes, new standalone types, namespaces and revamped extensions in order to not only help developers but to also improve overall security.

However, as with every update, there are also some functions that will become deprecated due to misconception, underutilization or outdated functionality and this PHP version does also have its fair share of such examples.

So without further ado, let’s dive right into some of the tweaks, changes and innovations that will come with PHP 8.2 at the end of the year!

Exciting new PHP features

As with every PHP version update, there are a lot of new and exciting features that will be added to the programming language! The following are some of the biggest changes that we can expect to see with the new - 8.2 version.

Making way for readonly classes

With the previous PHP version - 8.1, read-only class properties were introduced to the field, allowing developers to exert a much more granular approach when it comes to assigning classes and properties. A class with a ‘readonly’ property was capable of being initialized only once and making any changes, tweaks or adjustments to that class are strictly limited by the property.

This new version of PHP will allow developers to declare an entire class as ‘readonly’, which will help with both access, code management and security. Declaring a class as readonly will result in every subsequently declared member and instance to also be of the readonly type, meaning that they will not be able to be modified once created.

For a more detailed explanation on the new readonly classes, you can check out PHPWatch’s useful readonly classes documentation.

However, it was also announced that this will unfortunately result in PHP’s dynamic properties becoming deprecated. Not to worry though, as a new #[AllowDynamicProperties] attribute can be utilized as an alternative to this feature’s deprecation.

New standalone types - true, false and null

The release of PHP 8.0 brought Union types to fruition as a way of indicating that a given return value can be of multiple types. PHP 8.2 builds up on this concept by introducing a new standalone type - True, adding to the existing ones of False & Null, which were added in the previous update.

The new - ‘true’, type can be utilized in conjunction with the ‘false’ type, but only if appropriate and applicable, as well as anywhere else where the type wouldn’t be considered as redundant.

This redundancy can be easily checked, prior to assigning the type, by performing a simple check using the already-existing ‘bool’ type.

Also, it is important to mention that using a ‘true|false’ union isn’t supported as that is essentially the same as using the ‘bool’ type.

Revamped random namespace, extension & generator engines

PHP 8.2 also sees the introduction of a new ‘random’ extension, designed to give developers complete, thorough and meticulous control over how random numbers, strings and any information sequences are generated in addition to how exceptions are thrown and, consequently - dealt with.

This feature introduces a whole new ‘random’ namespace and class with all existing random number functions being relocated to this new extension. This allows developers to have a single place containing all of the functions that are going to be needed to design their applications, which will feature any form of randomness.

Furthemore, there are also a number of new engines that will be utilized for the process of generating random numbers - Mt19937, PcgOneseq128XslRr64, Xoshiro256StarStar & Secure with each one having different options that developers can choose to utilize for their projects.

MySQLi parameterized queries

PHP’s latest version also introduces a new ‘mysqli_execute_query’ function that will significantly improve how parameterized queries are executed.

This new feature allows developers to dramatically reduce the amount of code that they need to write in order to execute a multiple-parameter query.

The mysqli_execute_query is a function derived from the mysqli_execute method. Additionally, the new query function can also be used in conjunction with polyfill as well.

However, staying on the topic of MySQLi, the announcement has been made that as of the PHP 8.2, MySQLi will no longer be capable of being compiled using libmysql.

OpenSSL cipher keys can now return length

The next feature that the update brings is the ‘openssl_cipher_key_length’ function, which will provide developers with the ability to obtain the SSL key value in bytes of any OpenSSL cipher.

Similarly to the MySQLi changes, this feature can also work with polyfill, however, such a function would need to be manually hard-coded using the already known cipher lengths.

cURL extension - connections and information

Some changes have also been announced regarding the cURL extension. You can now make sure that all of your cURL connections are kept live by using the new curl_upkeep() function. However, this function is only available for a project running the cURLl extension that has been compiled using libcurl with a released version of 7.62 or later.

Another change that has been made to the extension is the fact that the ‘curl_getinfo’ function will now return the effective method of a request rather than the initial method of the request as those values might differ. If the function is called without specifying a parameter, it will return an array, containing information about the transfer.

Debugging, memory usage, parsing & permissions

Stack tracing and debugging has been made easier with the creation of a new PHP attribute - sensitive parameters. This ensures that the values of such tagged parameters will not leak into different error logs and general stack spaces.

Moreover, a new ‘error_log_mode’ INI setting will also be added, which will enable developers to configure error log access by changing the accessing permissions (you can also manually enable PHP error logs in order to review the errors).

In addition to this, constants are now also supported in PHP traits. Traits are similar to classes, however, their main intention is to help developers reuse code without having to create additional abstract classes, inheritance chains or interfaces. Similarly to traditional classes, trying to access a constant directly will result in an error message.

Next, PHP 8.2 also adds another new function - ‘memory_reset_peak_usage’, which allows developers to get the peak memory usage for each loop iteration or individual execution of a multiple-sequence action. Previously, developers could only get information about peak memory consumption over the span of the execution of the entire sequence, not every single iteration.

On top of this, a new non-capture modifier - ‘/n’ will also be implemented into the latest PHP release, allowing developers to avoid capturing any unexpected regular expression matches.

Finally, a new ‘ini_parse_quantity’ feature has also been added, allowing developers to now parse any data size supported by PHP, while returning this size in bytes, which will be extremely useful and save time when parsing INI values in any existing PHP application.

PHP 8.2 changes and bug fixes

In terms of changes, there aren’t that many things to discuss in detail. In general, there used to be a bug related to the ‘ksort’ function that would randomly sort character and numeric keys, placing them in a different order each time, regardless of the input.

This issue has now been addressed and the ‘ksort’ function will now always consistently place numerical keys in front of alphabetical ones when sorted.

The other implemented change is in relation to the ‘str_split’ function. The function has been tweaked to now return an empty array when used on an empty string.

However, there have been some other slight tweaks as well. Some examples of these smaller changes are the ‘iterable’ type becoming a built-in compile time alias for ‘Traversable’ arrays. There have also been changes to the DBA, Random & SPL functions as well as to the Date, Intl, Session, Tidy and Zip extensions. For more information about these tweaks you can visit PHP’s official manual.

Deprecated features in PHP 8.2

As we already discussed, the first feature to be discontinued is dynamic properties. The next deprecation in line is a number of pattern instances related to the $callable() pattern. You can find a full list of the no longer supported patterns by visiting the official PHP release statement.

Next, the ‘utf8_encode’ and ‘utf8_decode’ functions will also no longer be supported, starting from the 8.2 update. The reason behind this deprecation is that these functions were only capable of converting strings between ISO-8859-1 and UTF-8 encodings. The functions’ names, however, were creating a misconception, hence why support for them is being discontinued.

Sticking to the topic of encoding, the Mbstring extension’s ability to encode Base64, Uuencode, QPrint, and HTML Entity is also being deprecated. However, PHP does have a number of already built-in alternatives, which you can use to encode the aforementioned formats.

Finally, ${var} string interpolation has also been deprecated, meaning that developers now need to be inputting the $ sign directly in front of the variable like so - {$var}.

What does this update mean for the future of PHP?

Overall, PHP 8.2 will introduce a bunch of new features that will focus on security, ease of access and simplicity in order to aid developers in producing reusable and effective code and more secure projects and applications.

For more detailed information about the release and the different new and deprecated features as well as the aforementioned changes, you can also check out the official PHP 8.2 documentation upon release!

Subscribe to our newsletter.

Join 5000+ subscribers and receive helpful content, deals and more! We promise no spam - 100% great content. Unsubscribe anytime.

Deploy your server today!

Get started
Payment methods we accept:
Bitcoin
Lightning Network (Bitcoin)
Litecoin
Credit card
Bank transfer