PHP 8.2 is released with read-only classes, new stand-alone types, trait constants, and more – Laravel News

The PHP team has released PHP 8.2 today with read-only classes, new stand-alone types, a new random extension, trait constants, and more:

Readonly classes

Building on PHP 8.1’s read-only properties, marking a class as read-only makes every property on a class read-only and prevents the creation of dynamic properties.

1readonly class BlogData

2{

3 public…….

npressfetimg-533.png

The PHP team has released PHP 8.2 today with read-only classes, new stand-alone types, a new random extension, trait constants, and more:

Readonly classes

Building on PHP 8.1’s read-only properties, marking a class as read-only makes every property on a class read-only and prevents the creation of dynamic properties.

1readonly class BlogData

2{

3 public string $title;

4 

5 public Status $status;

6 

7 public function __construct(string $title, Status $status)

8 {

9 $this->title = $title;

10 $this->status = $status;

11 }

12}

Disjunctive normal form types (DNF)

DNF types combine union and intersection types :

1class Foo {

2 public function bar((A&B)|null $entity) {

3 if ($entity === null) {

4 return null;

5 }

6 

7 return $entity;

8 }

9}

Null, false, and true stand-alone types

PHP 8.2 now allows false, true, and null as standalone types. This example is pretty self-explanatory:

1class Falsy

2{

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

4 

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

6 

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

8}

Constants in traits

Constants are now allowed in traits. You cannot access constants through the name of the trait, however, you can access the constant through the class using the trait:

1trait Foo

2{

3 public const CONSTANT = 1;

4 

5 public function bar(): int

6 {

7 return self::CONSTANT; // Fatal error

8 }

9}

10 

11class Bar

12{

13 use Foo;

14}

15 

16var_dump(Bar::CONSTANT); // 1

Dynamic property deprecation

Dynamic properties is deprecated, meaning that you will get a deprecation notice when assigning a value to dynamic property:

1class User

2{

3 public $name;

4}

5 

6$user = new User();

7$user->last_name = 'Doe'; // Deprecated notice

8 

9$user = new stdClass();

10$user->last_name = 'Doe'; // Still allowed

You also have the option to allow dynamic properties using the AllowDynamicProperties attribute:

1#[AllowDynamicProperties]

2class User() {}

3 

4$user = new User();

5$user->foo = 'bar';

New classes, interfaces, attributes, and functions

PHP 8.2 contains new classes, interfaces, attributes, and functions. For the complete list, check out the New Classes, Interfaces, and Functions section of the PHP 8.2.0 Release Announcement.

We’ve already mentioned the AllowDynamicProperties attribute. Another attribute is the #[SensitiveParameter] attribute, which redacts sensitive information within a stack trace:

1function sensitiveParametersWithAttribute(

2 #[SensitiveParameter]

3 string $secret,

4 string $normal

5) {

6 throw new Exception('Error!');

7}

Learn more

To get up to speed on these new features, check out the PHP 8.2.0 Release Announcement page for examples before/after PHP 8.2, and view these links for more details on each item:

Source: https://news.google.com/__i/rss/rd/articles/CBMiImh0dHBzOi8vbGFyYXZlbC1uZXdzLmNvbS9waHAtOC0yLTDSAQA?oc=5