PHP 8.2 New Features and Improvements: All You Need to Note

Biztech

By : Biztech

Table of content

Table of content

PHP 8.2 New Features and Improvements: All You Need to Note1

Summary

PHP 8.2 was released on December 8, 2022. It introduces useful features like Type System Improvements, Sensitive Parameter Attributes, Fetch enum properties in constant expressions, Constants supported in traits, Readonly Classes, and Random Extension.

While the Readonly feature was earlier available for Class properties in PHP 8.1, the release of 8.2 version made sure the entire class can now be declared readonly. This means every class property now can be readonly when you declare the class as ‘readonly’.

That’s one of the many new features and improvements that the PHP 8.2 version has come with. In this blog, We will go through all of these one by one.

Read More: PHP development services

Salient PHP 8.2 Features to Note

 

1. Type System Improvements

  • PHP 8 introduced mixed type that is equivalent to the union type object|resource|array|string|int|float|bool|null.
  • PHP 8.1 introduces intersection types.
  • PHP 8.2 adds support for true, null, and false as stand-alone types. Prior to PHP 8.2, It was only possible to use null and false as parts of Union Types.

We can use null or false types as standalone types without being part of a union type from PHP 8.2. You can try below example:

< ?php
function isCompleted(): false {
echo 'Completed';
return false;
}
isCompleted();

Output:

Type System Improvements
Type System Improvements-2

2. Sensitive Parameter Attribute

This attribute is used to mark a parameter that is sensitive and should have its value redacted if present in a stack trace.

When a run-time exception is thrown during the execution of code It shows the sensitive information in the stack trace.

<?php
class Payment {
public function saveCardDetails(
string $cardNumber,
string $expiryDate,
string $cvv,
string $amount,
) {
throw new RunTimeException('Something went wrong, Please try again.');
}
}
(new Payment())->saveCardDetails('411111111111111', '12/2023', '123', '5000');

Output:

Sensitive Parameter Attribute

We can use #[\SensitiveParameter] with PHP 8.2 to hide sensitive information like card details as given in the below example.

class Payment {
public function saveCardDetails(
#[\SensitiveParameter] string $cardNumber,
#[\SensitiveParameter] string $expiryDate,
#[\SensitiveParameter] string $cvv,
string $amount,
) {
throw new RunTimeException('Something went wrong, Please try again.');
}
}
(new Payment())->saveCardDetails('411111111111111', '11/2023', '111', '5000');

Output:

Sensitive Parameter Attribute-2

3. Fetch enum properties in const expressions

We can use the enum properties value from PHP 8.2.

<?php
enum Status: string {
case Active = 'active';
case Deactive = 'deactivate';
case Deleted = 'deleted';
}
function status($value = Status::Active->value)
{
echo $value . PHP_EOL;
}
status();
status(Status::Deleted->value);

Output:

Fetch enum properties
Fetch enum properties

4. Constants in traits

We can allow defining constants in the trait before PHP 8.2. Trait constants can be defined in the same way as class constants. You can try the below example:

<?php
trait TaskStatus {
public const TODO = 'todo';
public const COMPLETED = 'completed';
public const IN_PROGRESS = 'in progress;
public function getList() {
return [self::TODO, self::IN_PROGRESS, self::COMPLETED];
}
}
print_r((new Upload())->getList());
print_r((new Upload())->getList());

Constants in traits
Constants in traits

5. Readonly classes

We can use read-only properties from PHP 8.1.

<php
class Post
{
public readonly string $title;
public readonly string $status;
public function __construct(string $title, string $status)
{
$this->title = $title;
$this->status = $status;
}
}
$blog = new Post('PHP 7.2 Features', 'In Progress');
$blog->title='PHP 7.1 Features';

Readonly classes

We can define a read-only class from PHP 8.2.

<?php
readonly class Post
{
public string $title;
public string $status;
public function __construct(string $title, string $status)
{
$this->title = $title;
$this->status = $status;
}
}
$blog = new Post('PHP 7.2 Features', 'In Progress');
$blog->title='PHP 7.1 Features';

Readonly classes

6. Random extension

The \Random\Randomizer class provides a high-level interface to use the engine’s randomness to generate a random integer, shuffle an array or string, to select random array keys, and more.

Generate a random number in an integer

$randomizer = new Random\Randomizer();
$randomizer->getInt(1, 50);

Shuffle an array

$randomizer = new Random\Randomizer();
$randomizer->shuffleArray(['1', '2', '3']);

Read also: Angular 14 Features

Conclusion

With the release of PHP 8.2, many new improvements have been added to the exclusive features released in PHP 8 and PHP 8.1. Some of the most noticeable improvements are the support for readonly class, new standalone types, sensitive parameter attribute, and constants in traits.

Getting acquainted with the new improvements will help you understand how PHP is evolving as a language and how you can improve the way you use it for coding. Furthermore, if you have a query regarding any of the new features or want expert consultation for your PHP project, we are just a few clicks away.

Talk to one of our PHP experts and get your queries answered right away!

Get a Free Consultation

    ✓ 100% Guaranteed Security of Your Information