PHP 8.2: What to Expect (Developer’s Guide – Updated)

PHP 8.2: What to Expect (Developer’s Guide – Updated) thumbnail

PHP 8.2 is expected to release in November this year, with the most recent stable version being PHP 8.1.5. While it’s still early in the process, there has been some mixed reception around PHP 8.2 and WordPress core, plugin, and theme compatibility.

However, knowing what to expect can help you prepare for the latest PHP version. By learning about the new features and the ones being deprecated, you can understand how the update may affect development. This knowledge can also help you get ready for the eventual release.

In this post, we’ll recap the most recent PHP versions. Then we’ll cover what’s new in PHP 8.2 and discuss the release schedule. Let’s jump in!

An Overview of PHP Versions

PHP is the primary programming language of WordPress. It plays a vital role in converting data from your database into HTML web pages for your site’s visitors.

As a website owner or developer, you’re likely already familiar with the importance of keeping your WordPress core, themes, and plugins updated. However, it’s also critical to keep your PHP up to date.

 

Currently, WordPress recommends using at least PHP 7.4. Older versions of PHP no longer receive active support for security updates and bug fixes. Therefore, upgrading to the latest stable version of PHP can result in better site security, enhanced performance, and higher levels of support (we emphasize stable because a segment of WordPress’s 60,000+ plugins aren’t fully PHP 8.0 compatible just yet, so it’s a good idea to approach these updates with some caution).

PHP 7.4 introduced typed properties, an underscore numeric separator, and various improvements. Since then, there have been a couple more iterations of PHP released.

PHP 8.0, released in November 2020, brought a handful of essential features. In addition to syntax and performance enhancements, the release included:

  • Named parameters
  • Match syntax
  • Union types
  • Constructor Property Promotion
  • JIT (which affects how PHP executes source code)

A year later came PHP 8.1, the latest major PHP version. This update includes significant features, such as:

  • Intersection types
  • Readonly properties
  • Enums
  • Fibers
  • Never return type

Staying on top of the latest versions of PHP can help boost the performance and security of your website. However, it’s important to know which changes to expect before upgrading. If you’re interested in testing the current state of PHP 8.2, you can do so via GitHub.

What’s New in PHP 8.2

PHP 8.2 is expected to be released by the end of 2022. Here is the current 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
  • November 24: GA

According to the official documentation on the PHP website, there should be a handful of new features and deprecated functions. Let’s take a look at some of them!

Disjunctive Normal Form (DNF) Types

PHP 8.2 will introduce a new Disjunctive Normal Form (DNF) types feature. DNF is a standardized way of organizing boolean expressions — specifically, into an ORed series of ANDs. When DNF is applied to type declarations, it enables a standard way to write combined Union and Intersection types that the parser can handle.

New mysqli_execute_query Function and mysqli::execute_query Method

Running parameterized MySQLi queries will be easier in PHP 8.2, thanks to the new mysqli_execute_query($sql, $params) function and mysqli::execute_query method.

This will further reduce the complexity of using parameterised queries, which will then make it easier for developers to move away from mysqli_query() as well as dangerous/risky escaping of user values.

Fetch enum Properties in const Expressions

According to the RFC, “This RFC proposes to allow the use of ->/?-> to fetch properties of enums in constant expressions. The primary motivation for this change is to allow fetching the name and value properties in places where enum objects aren’t allowed, like array keys.”

New Random Extension

PHP 8.2 introduces a new PHP extension named random. It organizes and consolidates existing PHP functionality related to random-number generation. It also introduces a series of PHP class structure and exception classes to provide granular options for random-number generators and exception handling.

The random extension is bundled with PHP, and there is no compile-time or run-time configuration option to disable the random extension. The random extension will be always available in PHP 8.2 and later.

New curl_upkeep Function

PHP 8.2 adds a new function to the Curl extension called curl_upkeep. The curl_upkeep function calls curl_easy_upkeep() in libcurl, which is the underlying C library that PHP Curl extension uses.

The curl_upkeep() function does the work that keeps a Curl connection alive. But not every connection type that Curl integrates supports connection upkeep. Currently, only HTTP/2 connections support it.

New ini_parse_quantity function

ini_parse_quantity is a new function that will be added to PHP in PHP 8.2. It parses any data size recognized by PHP INI values (such as 56K, 256M, or 1G), then returns the data size in bytes. It can be useful when parsing existing or provided INI values in a PHP application. However, it doesn’t recognize IEC suffixes for data sizes like MB, MiB, or GB, and it isn’t suitable to convert standard data size values to bytes.

New memory_reset_peak_usage Function

PHP 8.2 will include a new function called memory_reset_peak_usage. It will reset the peak memory usage returned by the memory_get_peak_usage function.

This function will be helpful for cases that involve invoking an action several times and recording the peak memory usage of each iteration. Developers will be able to use this new feature to reset the peak memory usage at any given point during the lifetime of the request.

Get Content Delivered Straight to Your Inbox

Subscribe to our blog and receive great content just like this delivered straight to your inbox.

Readonly Classes

Introduced in PHP 8.1, readonly properties will be expanded in PHP 8.2 to add syntactic sugar so that all class properties are readonly at once:

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

This will prevent dynamic properties from being added to classes.

Null, True, and False Standalone Types

In PHP 8.2, the return type of false will be available as a standalone type rather than strictly a union type for when an error occurs, which is already possible:

function alwaysFalse(): false
{
return false;
}

The same is true for the null type. For example, as a standalone type, unlike before, NullPost::getAuthor()will be able to only return null.

PHP 8.2 also allows true as a standalone type.

Deprecate Dynamic Properties

Dynamic properties will be deprecated in PHP 8.2, resulting in an ErrorException by PHP 9.0. These properties are set on an object:

class Post
{
public string $title;
}

// …

$post->name = 'Name';

Dynamic properties allow flexibility when creating classes (e.g., value objects) without a strict class declaration. Their deprecation will likely be an issue for developers who rely on dynamic properties because it will push them more toward static analysis. For this reason, some developers are apprehensive about this change with PHP 8.2.

However, classes using __get and __set will still support dynamic properties, as will objects of stdClass.

Alternatively, developers can use the new #[AllowDynamicProperties]attribute, declared in the global namespace, on classes to those properties:

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

While it’s not recommended, another option is to disable deprecation warnings.

New /n Modifier

PHP 8.2 will include support for the /n (no capture) modifier to the preg_* function family. When used, any groups with()meta-characters won’t capture apart from captured groups that are named. Essentially, the result is the same as marking each group as non-capturing.

The reasoning behind this change is that the modifier simplifies complex regular expressions for multiple groups. Rather than marketing every group as non-capturing, developers can mark all groups as non-capturing. Then, they can select and name specific groups that need to capture.

Redact Parameters in Back Traces

Many developers use services that track stack traces and production errors from codebases. These services can notify users if and when something goes wrong. For instance, tracing call stacks is helpful when debugging an application.

However, sometimes stack traces can have sensitive information, such as usernames and passwords. PHP 8.2 will include a #[SensitiveParameter] attribute that will prevent this information from being included in stack traces when things go wrong:

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

test('foo', 'bar', 'baz');

PHP 8.2 will use the sensitive parameters to redact private information from stack traces, making them more secure. These parameters ensure that the data won’t end up in error logs. Note that this attribute will only be available to use on parameters.

Deprecate ${} String Interpolation

There are multiple ways to embed variables in strings with PHP. However, PHP 8.2 will deprecate two methods. The first is using ${} in strings:

"Hello ${world}";

The second is using ${} (variable variables):

"Hello ${(world)}";

This likely won’t be a significant issue for developers as the two most popular string interpolation methods will still work.

Deprecate Partially Supported Callables

Another deprecated change will be with partially supported callables. There are multiple ways to create a callable in PHP. It can be called either with or without parameters with the $callable() syntax, the user_call_func(/a_array), or using a function with a callback.

The deprecated callable patterns include the following:

$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"];

Starting from PHP 8.2, calling any of the above will result in the following deprecation notice:

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";

However, passing these callables to the is_callable function or using them with the callable parameter types won’t generate the deprecation message. To prevent the deprecation notice, developers can instead convert parent, self, and static keywords in callable code to their respective class names using the::class magic method.

Part of the reason behind the change is to allow callables to be used for typed properties. It makes them less context-dependent.

MySQLi Can No Longer Be Compiled With libmysql

In the past, PHP has supported two libraries for connecting MySQL databases: mysqlnd and libmysql. As of PHP 5.4, the former has been the default library. However, it’s been possible to compile MySQLi through extensions.

Beginning with PHP 8.2, compiling a MySQLi extension with libmysql won’t be supported. Attempting to do so will result in a configuration error:

./configure --with-mysqli=FOO

Linking mysqli against external library is no longer supported

This won’t likely cause any significant errors for developers. However, the two biggest features supported by libmysql that aren’t available with mysqlnd are supported for automatic reconnecting and authentication through LDAP and SASL.

Keep Your WordPress PHP Version Up to Date

As a developer, it’s essential to test your code against upcoming versions of PHP. In this case, developers should begin testing their code against PHP 8.2 as soon as possible to ensure your code doesn’t break users’ sites when they update.

The Perfect Development Environment

We make sure your website is fast, secure and always up so your visitors trust you. Plans start at $1.99/mo.

shared hosting

About the Author:

Jason is DreamHost’s WordPress Product Advocate, based out of Bakersfield, CA. He is currently working on making our DreamPress product even better. In his free time, he likes to curl up on the couch and watch scary movies with his wife Sarah and three very small dogs. Follow him on Twitter.