PHP 8.2 Release is coming soon - What are the new features, deprecations, and improvements?

By: Skynet Technologies USA LLC
8 mins
500
PHP 8.2 release

PHP 8.2 is all set to hit the floor in November 2022. The latest version of PHP will bring several crucial improvements with refined performance and stability. Also, PHP 8.2 is likely to deprecate some features and add-ons that we are using for a longer span but they will be no longer supported in latest version.

Likewise, any other evolution, PHP 8.2 is expected to have more usable, vogue, and future-proof features. We need to get ready for this new release. It will be a technical shuffle for organizations to prepare themselves for PHP 8.2.

Let’s discuss PHP and its latest releases and upgrades.

PHP versions and their impact – an overview!

Many platforms use PHP as their primary programming language, including WordPress, Laravel, Symfony, etc. PHP helps in converting data from your database into HTML web pages for website visitors. Therefore, if you use a technology that runs on PHP, you ought to keep it updated with its latest version.

Currently, WordPress recommends using PHP 7.4 at least because older versions no longer get support. Similarly, for performance of the website, regular support, and better security practices of Laravel site, Laravel asks to use PHP 8.0.

PHP 8.0 was released in November 2020 with a handful of prominent features such as Named parameters, Match syntax, Constructor Property Promotion, etc. Then a year later, PHP 8.1 came into existence, which is the latest PHP version with the features like Intersection types, Enums, Fibers, never return type, read-only properties, etc.

Staying updated with the latest updates will boost the performance and security of your website. Moreover, if you will keep updating, a latest version might not be a problem for you.

What will be new in PHP 8.2?

PHP 8.2 is scheduled to be released by the end of 2022. There is a release schedule with General Availability (GA) programmed for November 24, 2022-

June 9: Alpha 1, June 23: Alpha 2, July 7: Alpha 3, July 19: Feature freeze, July 21: Beta 1, August 4: Beta 2, August 18: Beta 3, September 1: Release candidate 1, September 15: Release candidate 2, September 29: Release candidate 3, October 13: Release candidate 4, October 27: Release candidate 5, November 10: Release candidate 6, and November 24: GA.

So, this is how the latest version of PHP will release, as per the official documentation. It indicates myriad new features and some deprecations as well. Let’s learn about them.

1. Read-only classes

This property got introduced in PHP 8.1 and in 8.2 it will get an expansion to add syntactic sugar thereby, all class properties are read-only at once.

readonly class Post {
  public function __construct (
    public string $title,
    public Author $author,
    public string $body,
    public DateTime $publishedAt,
  ) {}
}

Readonly property prevents dynamic properties from being added to classes.

2. Null and False Standalone Types

The return type of false will be available as a standalone type in PHP 8.2 instead of strictly a union type for when an error occurs that is already possible.

function alwaysFalse(): false {
  return false;
}

This is true for all the null types.

3. New memory_reset_peak_usage Function

The latest version of PHP will have this vogue function called memory_reset_peak_usage. This function will reset the peak memory usage returned by the memory_get_peak_usage function. The new function will be useful for cases that involve bringing an action several times and storing the peak memory usage of every iteration.

This function can be used at any time during the lifetime of the request to reset the peak memory usage by developers.

4. Deprecated dynamic properties

Dynamic properties will be deprecated in PHP 8.2. Therefore, PHP 9.0 will get an ErrorException. These properties are set on an object.

class Post {
  public string $title;
}

// …
$post->name = 'Name';

Dynamic properties provide flexibility if you create classes without a strict class declaration. Thus, deprecation of this property will be a problem for developers who rely on them because now they will get pushed towards static analysis. That is why many developers are apprehensive about the new release of PHP this year.

However, classes that are using _get and _set will still support dynamic properties. On the other hand, developers will be able to use the new #[AllowDynamicProperties]attribute, declared in the global namespace, on classes to those properties:

#[AllowDynamicProperties]
class User() {}
$user = new User();
$user->foo = 'bar';

Another option that can be used is to disable deprecation warnings, though it is not recommended.

5. Redact parameters in back traces

Tracking stack traces and production errors from codebases these are the services used by myriad developers. The services notify developers if anything goes wrong in an unfavourable way, such as tracing call stacks are useful if debugging an application.

At times, stack traces may have critical information such as usernames and passwords. PHP 8.2 will include a #[SensitiveParameter]attribute, which will protect such sensitive information from being included in stack traces if anything goes wrong.

function test(
  $foo,
  #[\SensitiveParameter] $bar,
  $baz
) {
  throw new Exception('Error');
}
test('foo', 'bar', 'baz');

By using sensitive parameters, PHP 8.2 will redact confidential information from stack traces and keep the information secured. The redact parameters ensure that the important data will not end up in error logs.

6. New /n modifier

/n (no capture) modifier to the preg_* function family will get support in PHP 8.2. If it will get used, any groups with () meta-characters will not capture apart from captured groups that are named. The aftermath is the same as marking every group as non-capturing.

The modifier simplifies complex regular expressions for multiple groups and that is the reason behind this change. Instead of marketing all the groups as non-capturing, developers can mark every group as non-capturing. And afterward, developers can select and name specific groups that are supposed to be captured.

7. Deprecated ${} String Interpolation

There are myriad ways to embed variables in strings with PHP, but the version PHP 8.2, will deprecate two methods of embedding variables.

First is using ${} in the string: “Hello ${world}”;

And second is using ${} (variables variables): “Hello ${ (World) }”;

This deprecation will not be a problem for developers because other methods will still work.

8. MySQLi can no longer be compiled with libmysql

PHP was supporting two libraries for connecting MySQL databases: mysqlnd and libmysql. Till PHP 5.4, the former was a default library. However, extensions allow compilation of MySQLi.

PHP 8.2 and onwards, compiling MySQLi with libmysql will not be possible. Attempts will only give configuration errors.

./configure --with-mysqli=FOO

Linking mysqli against an external library will no longer be supported in PHP 8.2.

9. Deprecate partially supported callables

A few partially supported callables are also deprecated in PHP 8.2. The deprecated callable patterns are:

$callable = "self::method";
$callable = "parent::method";
$callable = "static::method";
$callable = ["self", "method"];
$callable = ["parent", "method"];
$callable = ["static", "method"];
$callable = ["MyClass", "MyParentClass::myMethod"];
$callable = [new MyClass(), "MyOtherClass::myMethod"];

If any of the above will get used in PHP 8.2, the following deprecation notice will appear.

class Test {
  public static function myMethod(): void {
    echo "Called";
  }
  public static function call(): void {
    $callable = 'self::myMethod';
    call_user_func($callable);
  }
}

$callable = Test::call();
// "Called";

But passing these callables to the is_callable function or using them with the callable parameter types will not generate the deprecation message. Developers can convert parent, self, and static keywords in callable code to their respective class names using the::class magic method to avoid the deprecation notice. The reason behind this change is to allow callables to be used for typed properties.

Wrapping up

Now you know what all latest changes might come with new PHP 8.2. Thus, start preparing accordingly. Developers must test their code against the forthcoming PHP 8.2 to make sure that website will not break down with the new PHP version or hire an experienced PHP developer for seamless PHP site upgradation.

Our PHP developers are proven delivering custom web solution whether it’s a simple web solution, complex enterprise level application, ecommerce solution, CMS site and more. As a leading PHP development company, we build robust and scalable PHP web application that streamline business process and add value to your business. Whether you require new PHP website, upgrade your current website to any latest version of PHP 8.x; or want to migrate your website to PHP 8; We provide one-stop-solution. Get in touch with us via email us at [email protected] or fill out the following form to know more.

641