What's new in PHP 8.2

  • avatar
  • 1.1K Views
  • 2 Likes
  • 5 mins read

PHP 8.2 has been released on December 8, 2022. We'll go through most important features, performance improvements, changes and deprecations one by one.

New read only classes

Read only properties were introduced in PHP 8.1. Now, PHP 8.2 is adding support to declare the entire class as read only.

If you declare a class as read only, all its properties will automatically inherit the read only feature. Thus, declaring a class read only is the same as declaring every class property as read only.

Instead of writing this:

class HiBitClass
{
public readonly int $intValue
public readonly string $stringValue
}

You can now write:

readonly class HiBitClass
{
public int $intValue
public string $stringValue
}

You can also declare a read only class with no properties. This prevents dynamic properties while still allowing child classes to declare their read only properties explicitly.

Deprecate dynamic properties

In previous PHP versions you could dynamically set and retrieve undeclared class properties in PHP:

class User {
private int $id;
}

$user = new User();
$user->name = 'HiBit';

Dynamic properties allow unexpected bugs and behavior in the code. From PHP 8.2 onward setting a value to an undeclared class property will emit a deprecation notice. Keep in mind that classes implementing getters and setters will still work as intended.

Redact sensitive parameters in back traces

Some projects use third-party services that track stack traces and production errors. These stack traces may include sensitive information such as usernames, passwords and/or other unwanted variables.

PHP 8.2 allows you to mark such sensitive parameters. Any parameter marked sensitive will not be listed in your back traces.

function login(
$username,
#[\\SensitiveParameter] $password
) {
...
throw new \\LoginFailed('Something went wrong');
}

login('my_username', 'my_password');

When generating a back trace, any parameter with the SensitiveParameter attribute will be replaced and its real value will never be stored in the trace.

Disjunctive normal form types

DNF types allow us to combine union and intersection types, following a strict rule: when combining union and intersection types, intersection types must be grouped with parentheses.

function printOutput((WordInterface&PrintableInterface)|string $text): void
{
echo $text;
}

Allow Constants in Traits

Currently, traits only allow defining methods and properties but not constants. This RFC proposes to allow defining constants in traits. These constants can be defined just like you’d define class constants.

trait MyTrait {
private const FLAG_1 = 1;
private const FLAG_2 = 2;

public function getFlag(int $flag): string
{
return match ($flag) {
self::FLAG_1 => 'FLAG 1',
self::FLAG_2 => 'FLAG 2',
default => 'Unknown flag',
};
}
}

Allow true, false and null as standalone types

PHP already includes scalar types and technically null, true and false could be considered valid types on their own too.

function alwaysFalse(): false
{
/* ... */
}

function alwaysTrue(): true
{
/* ... */
}

function alwaysNull(): null
{
/* ... */
}

Common examples are PHP built-in functions, where false is used as the return type to represent errors.

Fetch enum properties in const 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. It means the following code is now valid:

const B = [self::A->value => self::A];

New MySQL functions

Essentially, there is a new mysqli_execute_query() function that is a combination of mysqli_prepare(), mysqli_execute() and mysqli_stmt_get_result() functions. With it, the MySQLi query will be prepared and executed within the same function. If the query runs successfully, it will return a mysqli_result object, otherwise it will return a false.

Deprecate utf8_encode and utf8_decode functions

Using either utf8_encode() or utf8_decode() will trigger deprecation notices in PHP 8.2. The RFC argues that these functions have a inaccurate name that often causes confusion: these functions only convert between ISO-8859-1 and UTF-8, while the function name suggest a wider usage.

 Join Our Monthly Newsletter

Get the latest news and popular articles to your inbox every month

We never send SPAM nor unsolicited emails

0 Comments

Leave a Reply

Your email address will not be published.

Replying to the message: View original

Hey visitor! Unlock access to featured articles, remove ads and much more - it's free.