What’s New In PHP 8.2: New Features and Updates

Palak
6 min readDec 14, 2022

Initially known as the Personal Home Page, PHP or Hypertext Preprocessor is an open-source programming language used for server-side scripting of web applications. It offers extensive libraries for common functions to cut down the code writing time. Thanks to its user-friendly attributes and extensive scope of capabilities, PHP quickly became popular, so much so that more than 75% of websites use it for server-side programming.

Latest Features of PHP 8.2

True, False, and Null as Standalone Types

In the previous version of PHP, you could easily declare a particular type as the unification of two or more individual types. Although false and null were usable as types, you couldn’t use them as standalone types. But now, with PHP 8.2, you can use them as standalone types, making PHP more descriptive with the help of more precisely declared parameters, returns, and property types. Here is an example of false being used as a standalone type:

function alwaysFalse(): false
{
return false;
}

Disjunctive Normal Form Types

PHP 8.2 lets you use the new Disjunctive Normal Types feature to organize the boolean expressions. It consists of a boolean OR of ANDs. Here’s an example of DNF types:


// Accepts an object that implements both A and B,
// OR an object that implements D.
(A&B)|D
// Accepts an object that implements C,
// OR a child of X that also implements D,
// OR null.
C|(X&D)|null
// Accepts an object that implements all three of A, B, and D,
// OR an int,
// OR null.
(A&B&D)|int|null

Constants in Traits

PHP 8.2 also introduced a way to reuse the codes in Traits by defining the properties and methods, which is excellent for code reuse across classes. This version lets you define constants in Traits.

Here is an RFC proposal for an example:

trait Foo
{
public const CONSTANT = 1;
}

class Bar
{
use Foo;
}

var_dump(Bar::CONSTANT); // 1
var_dump(Foo::CONSTANT); // Error

Readonly Classes

Although the previous version had a readonly feature for class properties, the latest version of PHP has also introduced support for the entire class to be defined as readonly. Declaring a class readonly means all properties of that class will be readonly as well.

For example:

readonly class MyClass
{
public string $myValue,
public int $myOtherValue
public string $myAnotherValue
public int $myYetAnotherValue
}

In this piece of code, you can see the whole class is defined as readonly. So all properties and elements are in the readonly mode. This version of PHP also lets you define the final or abstract classes as readonly. Other than that, you can define a class without any properties as , readonly which prevents the main class from having dynamic properties while declaring the child classes’ properties as readonly explicitly.

Redacting The Support For Sensitive Parameter Values

With this feature, you can track the call stack at any point in a program. You can also debug your application and see why there are errors. But there may be certain stacks in your program containing sensitive information. With this version 8.2, you can easily mask this information via a feature called, which will help prevent this sensitive info from being logged in case of any error.

Here’s an example of the SensitiveParameter feature:

function passwords(
$publicpassword,
#[\SensitiveParameter] $secretpassword
) {
throw new \Exception('Error');
}
passwords('publicpassword', 'secretpassword');

PHP has introduced these features in the latest version to simplify and enhance the developmental process of web applications.

New Functions and Classes of PHP 8.2

New mysqli_execute_query Function and mysqli::execute_query Method

This new version of PHP introduced a better and smoother way to take care of the precomputed MySQLi queries. Through the mysqli::execute_query method and mysqli_execute_query function, you can prepare, define and execute the necessary queries within the same function.

The new function introduced in PHP 8.2 is combined using the mysqli_prepare(), mysqli_execute(), and mysqli_stmt_get_result() functions, and with it, the MySQLi will be executed from within.

Here is an RFC proposal for an example:


foreach ($db->execute_query('SELECT * FROM user WHERE name LIKE ? AND type_id IN (?, ?)', [$name, $type1, $type2]) as $row) {
print_r($row);
}

Parse INI quantity values: ini_parse_quantity

The ini_parse_quantity function reads any data size that PHP INI values can recognize, such as 56K, 256M, or 1G, and returns the size of the data in bytes.

ini_parse_quantity('256M'); // 268435456

Maintain an existing Curl connection: curl_upkeep

The curl_upkeep function in the PHP 8.2 Curl extension tells the underlying Curl library to do what it needs to do to keep a Curl connection alive. The most common way to use this function is to call curl _upkeep periodically to keep an HTTP persistent connection (Keep-Alive) alive.

Get the Length of the Cipher Key: openssl_cipher_key_length

In PHP 8.2 OpenSSL, there is a new function called openssl_cipher_key_length that returns the required length of the key (in bytes) for any OpenSSL cypher.

This function gets rid of the need for OpenSSL cypher operations to hard-code the length of the key they need.

openssl_cipher_key_length("CHACHA20-POLY1305"); //32 
openssl_cipher_key_length("AES-128-GCM"); //16
openssl_cipher_key_length("AES-256-GCM"); // 32

view rawCipher Key length hosted with ❤ by GitHub

memory_reset_peak_usage – Reset recorded peak memory usage

memory_reset_peak_usage is a new function in PHP 8.2 that resets the peak memory usage reported by the memory_reset_peak_usage function.

This can be useful in applications that require recording the peak memory usage of each invocation after invoking or iterating an action numerous times. memory_reset_peak_usage returns the absolute peak memory usage throughout the entire run without the opportunity to reset the memory consumption with the new memory_reset_peak_usage function.

Deprecated Features of PHP 8.2

Here are the features that existed in the previous versions of PHP but are not anymore:

Mbstring functions for QPrint/HTML Entities/Base64/Uuencode

Mbstring or multi-byte string functions are used to convert to and from the different coding standards like ISO-8859-1 and UTF-8/16/32, along with Quoted-Printable, HTML entities, Base64, and Uuencode. But PHP 8.2 has deprecated the mbstring extensions to these labeled encodings.

${var} String Interpolation

With the earlier versions of PHP, you can replace the variable values within a string literal with double quotes. For example:

"{$myname}" – braces outside the variable.
"${expr}" – variable variables equivalent to using (string) ${expr}
"$myname" – directly embedding variables.
"${myname}" – braces after the dollar sign.

While ensuring the same functionality, the last two syntaxes are quite complex. That’s why they will be deprecated in PHP 8.2.

#utf8_encode() and utf8_decode()

These functions can be used to convert to and from between UTF-8 and ISO-8859–1 encoding standards. Although these functions will be deprecated in this version, they will be removed altogether in the PHP 9.0.

Partially-Supported Callables

The partially-supported callables will be deprecated by PHP 8.2, so they won’t work with the $callable() patterns.

Here are a few of the deprecated callables:

$callable = "parent::method";
$callable = ["self", "method"];
$callable = ["static", "method"];
$callable = [new MyClass(), "MyOtherClass::myMethod"];
$callable = "self::method";
$callable = "static::method";
$callable = ["parent", "method"];
$callable = ["MyClass", "MyParentClass::myMethod"];view rawdeprecated callables hosted with ❤ by GitHub

To prevent the deprecation, you can convert all of the parent, self, and static keywords to their corresponding class names.

These deprecated features are usually not needed in web application development of the web applications without the impact on the features.

Upgrade your PHP Version

Like any other popular web framework, people can have some issues with PHP during application development. But it receives regular updates, the newest one of which is Version 8.2. Not only has this update introduced new features to improve the functionality and security of the application, but it also eases its development process.

So if you are not trained or experienced in programming, it is very important you can take help from hiring expert PHP developers to upgrade your PHP to its latest version.

Share this story, and choose your platform! Read the Full Blog on What's New in PHP 8.2

--

--

Palak

Writer | Blogger| Reading| Passion for Writing about Programming and Tech