Ad Code

Responsive Advertisement

Ticker

6/recent/ticker-posts

What’s New in PHP 8.1: Features, Changes, Improvements, and More

 PHP 8.1 is a major update of the PHP language.

It contains many new features, including enums, read-only properties, first-class callable syntax, fibers, intersection types, performance improvements, and more.


Official websites of PHP: go to php official website



Enumerations:

Create Enum also known as Enumeration.


Before Enum we are creating Enum like that:

class Status
{
    const 
DRAFT 'draft';
    const 
PUBLISHED 'published';
    const 
ARCHIVED 'archived';
}
function 
acceptStatus(string $status) {...}



Now we are creating like that

Use enum instead of a set of constants and get validation out of the box.

Syntax:

enum Status
{
    case 
Draft;
    case 
Published;
    case 
Archived;
}
function 
acceptStatus(Status $status) {...}


you can access it like that Status::Draft;






Post a Comment

0 Comments