What's New in PHP 8.2 ?



PHP 8.2 is the latest major release of the popular scripting language that powers millions of websites. It brings many new features and improvements that make PHP more expressive, powerful, and performant. here, lets some of the most notable new features in PHP 8.2 and how they can benefit to php devs.

 

 

Readonly Classes


One of the new features in PHP 8.2 is readonly classes . Readonly classes are classes that have all their properties declared as readonly by default. This means that you can only assign values to their properties once, during construction, and you cannot modify them afterwards.

Readonly classes are useful for creating immutable value objects that represent some data without any behavior. For example, you can create a readonly class to represent a blog post:

 

php
readonly class BlogPost {
public string $title;
public string $content;
public DateTimeImmutable $createdAt;

    public function __construct(string $title, string $content) {
        $this->title = $title;
        $this->content = $content;
        $this->createdAt = new DateTimeImmutable();
    }

 

Now you can create instances of BlogPost and pass them around without worrying about someone changing their properties:
 


$post = new BlogPost("What's New in PHP 8.2", "PHP 8.2 is awesome!");
echo $post->title; // What's New in PHP 8.2
echo $post->content; // PHP 8.2 is awesome!
echo $post->createdAt->format('Y-m-d H:i:s'); // 2023-03-22 14:07:45

// Trying to change any property will result in a fatal error:
$post->title = "Something else"; // Fatal error: Uncaught Error: Cannot modify readonly property BlogPost::$title


Readonly classes can also be abstract or final, and they can only contain typed properties (you can use mixed if you don't know the type).


Disjunctive Normal Form Types


Another new feature in PHP 8.2 is disjunctive normal form (DNF) types . DNF types allow you to combine union and intersection types using parentheses to group them logically.

Union types are types that represent a value that can be one of several different types. For example, string|int means a value that can be either a string or an integer.

Intersection types are types that represent a value that must satisfy multiple type constraints at the same time. For example, Countable&Traversable means a value that must implement both Countable and Traversable interfaces.

DNF types allow you to express complex type constraints using both union and intersection types together. For example, (A&B)|null means a value that can be either null or an instance of both A and B classes.

Here is an example of how you can use DNF types to type-hint a function parameter:
 

<?php


class A {}
class B {}
class C {}

function foo((A&B)|null $x) {
if ($x === null) {
echo "x is null\n";
} else {
echo "x is an instance of A and B\n";
}
}

foo(null); // x is null
foo(new A()); // TypeError: Argument 1 passed to foo() must be an instance of A&B or null, instance of A given
foo(new B()); // TypeError: Argument 1 passed to foo() must be an instance of A&B or null, instance of B given
foo(new C()); // TypeError: Argument 1 passed to foo() must be an instance of A&B or null, instance of C given
foo(new class extends A implements B {}); // x is an instance of A and B


DNF types follow a strict rule when combining union and intersection types: intersection types must be grouped with parentheses when used with union types.

 

 

 Allow true, false, and null as Standalone Types


PHP 8.2 also allows true, false, and null as standalone types . This means that you can declare a function return type or a parameter type as one of these literal values.


Redact Sensitive Parameters in Back Traces: 

PHP 8.2 introduces a new function called debug_backtrace_with_args() that allows you to redact sensitive parameters from back traces.


New mysqli_execute_query Function and mysqli:

 

execute_query Method: A new function called mysqli_execute_query() has been added to the mysqli extension in PHP 8.2.

Fetch enum Properties in const Expressions: 

You can now fetch enum properties in const expressions.
Allow Constants in Traits: You can now define constants in traits.


find more from oficial doc: PHP: PHP 8 ChangeLog

Overall, PHP 8.2 is an exciting update to the PHP language that brings many new features and improvements. Whether you’re a seasoned PHP developer or just getting started with the language, these new features are sure to make your life easier and your code more efficient. From readonly classes to disjunctive normal form types, there’s something for everyone in this update



















Comments

Popular posts from this blog

Handling undefined variables in laravel

How to use Spatie packages to enhance your Laravel development experience

Some Briefs of Data Mining