1009
PHP 8.2 New Features and Improvements: All You Need to Note
08 Feb, 2023
7 min read
1009
08 Feb, 2023
7 min read
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
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:
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:
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:
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:
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());
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';
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';
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
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!