PHP 8.2: What are its New Features?

We will cover all the new updates in PHP 8.2 including new features and improvements. So if you are excited to unlock its features then just glue your eyes here.  

If you are a professional PHP developer then it is utmost important for you aware about new features in this version.

PHP 8.2 Features

PHP 8.2 Features: What's New?

1. New read-only class property:

There were read only properties present in PHP 8.1. Now it is possible to declare an entire class as read only in 8.2.

When the entire class is read only then all the properties in it will automatically inherit read only properties.

To declare the entire class as readonly, we have to add it in every variable inside the class.

class MyClass
{
    Public readonly string $ItemName, 
    Public readonly int $count, 
    Public readonly string $SellerName, 
    Public readonly int $price
} 

With 8.2 you can simply add readonly in the class name.

class readonly MyClass 
{
  public string $itemName, 
  public int $count, 
  public string $sellerName, 
  public int $price
} 
Also Read

2. Constants in Traits:

Traits are like class with the only difference about their use. They (traits) are used for only grouping methods in the consistent manner.

You can only declare the methods inside the trait. You can now define constants in new PHP 8.2 improvement.

Trait foo
{
  Public const CONSTANT =1;
  Public function bar():  int
  {
      Return self::CONSTANT; 
}
} 

You cannot access the constant through traits name.

Trait Foo
{
  Public const CONSTANT = 1;
  Public function bar (): int
{
  Return Foo :: CONSTANT;
}
}
 Foo::CONSTANT; 

You can also access the publicly declared constant through the class which uses the trait.

class MyClass
{
 Use Foo;
}
MyClass:: CONSTANT;  //1 

3. Fetch enum Properties in const Expressions:

This RFC allow the use of -> / ?->. In PHP new version, it fetches properties of enums in constant expressions.

It enables the enum value and name properties to be retrieved in the array key. The enum case value cannot be expressed without repeating in the current implementation.

enum A:  string
{
  case B = ‘B’;
  const C = [self::B->value => self:: B];
} 

4. Disjunctive Normal Form (DNF) Types:

DNF is the process to organize Boolean expressions which will involve the disjunctions between the conjunctions.

Here we can use the type declaration written in the DNF for combining the Union and Intersection type. It can be parsed by the parser. A new DNF types feature in the PHP 8.2, can make it powerful.

This DNF will include the property declaration, return value and parameter declaration.

// Accept both A and B object
// OR objects that implement D

(A&B) |D

//Accepts objects implementation C, 
//Or a child of X make implements D, 
//OR null 

C|(X&D) |null
//Accepts objects that make all three of A, B and D,
//OR an int,
//OR null
(A&B&D)|int|null 

5. Redact Sensitive Parameters in Back Traces:

Sending production errors to the service is a common practice in any codebase. It will notify developers when anything goes wrong. It also involves the sending stack traces over the wire to the third party service. It contains some sensitive information like password, variable and username.

In PHP 8.2 you will be able to mark such sensitive parameters with the attribute. It will prevent the listing in the stack traces when something goes wrong.

Function login(
String $user, 
#[\sensitiveParameter]  string  $password
){
   //…

throw new Exception(‘Error’);
}
login(‘root’,’root’); 

6. New ini_parse_quantity functions:

The new function in PHP 8.2 that is ini_parse_quantity will parse any size of data. It will return as several bytes in the particular format.

You can parse an INI value provided or already existing in the PHP application.

function ini_parse_quantity(string $shorthand): int {
} 

The ini_parse_quantity function will parse the last character suffix in the given $shorthand.

To calculate the return value, the returned value will be multiplied by the specified unit.

Also Read

7. Curl: CURLINFO_EFFECTIVE_METHOD support in curl_getinfo function:

The curl_getinfo function will return the information about the specific Curl transfer.  They have added a support in PHP 8.2 that will return the effective method of request.

Without specifying any parameter in the curl_getinfo, by default, it will return an array of all information related to the Curl transfer.

There will be an extra element in the array with the key effective_method. It will contain the effective method name of HTTP request.

Usage

$ch = curl_init();
Curl_setopt ($ch, CURLOPT_URL, ‘https://myshop.com’);
Curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
Curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Curl_exec($ch);

$info = curl_getinfo($ch);
Var_damp ($info); 

You can pass the CURLINFO_EFFECTIVE_METHOD constant to the curl_getinfo function. It calls to the specifically return the effective_method.

$ch = curl_init();
Curl_setopt($ch, CURLOPT_URL, ‘https://myshop.com’);
Curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
Curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
Curl_exec($ch);


$info = curl_getinfo($ch, CURLINFO_EFFECTIVE_METHOD);
Var_dump($info); 

8. Allow Dynamic properties attribute:

PHP 8.2 has come with #AllowDynamicProperties as a new attribute that help to avoid the deprecation notice for the dynamic properties.

#[AllowDynamicProperties]
Class User() {}
$user = new User ();
$user->username =’Andrew’; 

It doesn’t accept any parameter and declared in the global namespace.

9. Allow true, false, and null as Standalone Types:

PHP already include scalar types like string, int and bool. With the addition of union types, it got expanded in PHP 8.0. It allows values to be of different types. The same RFC also allowed using false and null as union type but were not allowed as standalone type.

The result will be in a fatal error if you declare false or null or as a standalone types.

Function spam(): null {}
Function eggs(): false {}
Fatal Error:  Null is not allowed to use as a standalone type 
Fatal Error: False is not allowed to be used as a standalone type 

PHP 8.2 comes with supporting of false and null as a standalone type. The type system of PHP is more expensive and complete. It is now easy to declare parameter, return and property types precisely.  

Verdict:

PHP 8.2 comes with many new features and many other improvements over PHP 8.0 and PHP 8.1. Readonly properties and its new standalone types are some of its eye catchy properties. Professional developers can find out much improvement in this PHP latest version.