What’s new in PHP 8.2

Parvej Ahammad
3 min readJun 3, 2022

--

PHP is evolving with every single release. It’s not the same language we all knew. PHP updated itself drastically within the release of PHP7 back in December 2015. It was almost 150% performance gain compared to PHP5. PHP is adopting more and more modern language syntax and proved itself as a modern scalable language. By the release of version 8.0, it became a very mature language. PHP 8.2 is also no exception. It is scheduled to release on November 24, 2022. In this article, we will discuss what features are accepted for version 8.2.

Readonly Classes

PHP 8.1 added support for readonly properties. But still, it is not possible to create readonly classes. With PHP 8.2 you can create readonly classes which will make all properties of that class implicitly readonly. It will also prevent creation of dynamic properties.

Code example taken from RFC

Note: AllowDynamicProperties attribute will not be allowed in readonly class.

MySQLi Execute Query

PHP 8.1 solved the problem of bindings SQL parameter by reference.

Code example taken from PHP RFC

PHP 8.2 will make it much easier with the exclusion of the execute_query method in Mysqli.

Code example taken from PHP RFC

PHP 8.2: No-capture modifier (/n) support in preg_* functions

PHP 8.2 adds support for the /n (no capture) modifier to preg_* family of functions. When the /n modifier is used in a regular expression, all non-named groups do not capture by default.
We all know that `()` denotes capturing group of regex in php. In PHP 8.2 if you add \n at the end of regex all the capturing group without name all not be capture by regex.

Without \n

Code example taken from PHP RFC

With \n

Code example taken from PHP RFC

New memory_reset_peak_usage function

memory_reset_peak_usage that resets the peak memory usage returned by the memory_get_peak_usage function. This can be helpful in applications that invoke or iterate an action multiple times and needs to record the peak memory usage of each invocation. Without the ability to reset the memory usage with the new memory_reset_peak_usage function, the memory_get_peak_usage returns the absolute peak memory usage throughout the entire run.

Code example taken from PHP RFC

AllowDynamicProperties Attribute

PHP 8.2 will deprecate dynamic properties from class. Previously we could assign any property to a class without being define in the class.

Code example taken from PHP RFC

That will not be possible anymore in PHP 8.2. PHP 8.2 will emmit an depecation notice if you try to set dynamic property in class.

Code example taken from PHP RFC

But some application are havily rely on this dynamic property set and get. Like some orm and query builder. To overcome this deprication php 8.2 introduce `AllowDynamicProperties` attributes in class. If you add this in your class you can set and get dynamic property in you class like you used to

Code example taken from PHP RFC

PHP 8.2 bringing some good features for PHP developers. Also making the language modern day by day. You can learn all about PHP8.2 here

--

--