In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the meaning and usage of PHP enumeration". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn the meaning and usage of PHP enumeration.
As usual, using my PHP function post, let's start with a high-level overview of what the enumeration looks like:
Enum Status {case DRAFT;case PUBLISHED;case ARCHIVED;}
The advantage of enumerations is that they represent a collection of constant values, but most importantly, you can enter these values, such as:
Class BlogPost {public function _ _ construct (public Status $status,) {}}
In this example, an enumeration is created and passed to blogpost as follows:
$post = new BlogPost (Status::DRAFT)
This is the basic way, because you can see that there are no complicated things.
# enumeration methods
Enumerations can define methods, just like classes. This is a very powerful function, especially when used with the match operator:
Enum Status {case DRAFT;case PUBLISHED;case ARCHIVED; public function color (): string {return match ($this) {Status::DRAFT = > 'grey', Status::PUBLISHED = >' green', Status::ARCHIVED = > 'red',};}}
Method can be used as follows:
$status = Status::ARCHIVED;$status- > color (); / / 'red'
Allow static methods:
Enum Status {/ / … Public static function make (): Status {/ / … }}
You can also use self:
Enum Status {/ / … Public function color (): string {return match ($this) {self::DRAFT = > 'grey', self::PUBLISHED = >' green', self::ARCHIVED = > 'red',};}} # enumerate interfaces
Enumerations can implement interfaces, just like normal classes:
Interface HasColor {public function color (): string;} enum Status implements HasColor {case DRAFT;case PUBLISHED;case ARCHIVED; public function color (): string {/ * … * /} # enumerated values
Enumerated values are represented by internal objects, but you can assign them a value if you prefer
Enum Status: string {case DRAFT = 'draft';case PUBLISHED =' published';case ARCHIVED = 'archived';}
Note the type declaration in the enumeration definition. It indicates that all enumerated values are of a given type. You can also make it int. Note that only int and String are allowed as enumerated values.
Enum Status: int {case DRAFT = 1 per case PUBLISHED = 2 per case ARCHIVED = 3;}
The technical term for typing enumerations is called "backed enums" because they are given the simpler value "backed". If you decide to assign enumerated values, all cases should have values.
# enumerations with interfaces
If you are merging supported enumerations and interfaces, the enumeration type must come directly after the enumeration name and before the Implements keyword.
Enum Status: string implements HasColor {case DRAFT = 'draft';case PUBLISHED =' published';case ARCHIVED = 'archived'; / / … } # Serialization enumeration
If you want to assign values to enum, you may need a way to serialize and deserialize them. Serialization means that you need a way to access enumerated values. This is done through read-only public properties:
$value = Status::PUBLISHED- > value; / / 2
You can do this by using Enum::from
$status = Status::from (2); / / Status::PUBLISHED
There is also a tryfrom that returns null if an unknown value is passed. There will be exceptions if you use from.
$status = Status::from ('unknown'); / / ValueError$status = Status::tryFrom (' unknown'); / / null
Note that you can also use the built-in serialization (serialize) and deserialization (unsermalize) functions for enumerations. In addition, you can use json_encode with supported enumerations, and the result will be an enumeration value. You can override this behavior by implementing JsonSerializable.
# Listing enum values
You can use the static Enum::cases () method to get a list of all available values in the enumeration:
Status::cases (); / * [Status::DRAFT, Status::PUBLISHED, Status::ARCHIVED] * /
Note that this array contains the actual enumerated objects:
Array_map (fn (Status $status) = > $status- > color (), Status::cases ())
When using supported enumerations, the array key contains enumerated values:
Status::cases (); / * ['draft' = > Status::DRAFT,' published' = > Status::PUBLISHED, 'archived' = > Status::ARCHIVED,] * / # enumerate objects
I have mentioned that enumerated values are represented as objects, but these are actually singleton objects. This means that you can compare with them:
$statusA = Status::PENDING;$statusB = Status::PENDING;$statusC = Status::ARCHIVED;$statusA = $statusB; / / true$statusA = $statusC; / / false$statusC instanceof Status; / / true# enumeration as array key
Because enumerated values are actually objects, they cannot be used as array keys at this time. The following will result in an error:
$list = [Status::DRAFT = > 'draft',//...]
This means that you can only use enumerations as keys in SplObjectStorage and WeakMaps.
# Traits
Enumerations can use features like classes, but with more restrictions. You are not allowed to override built-in enumeration methods, and they cannot contain class properties-these properties are prohibited in enumerations.
# Reflection and attributes
As expected, there are several reflection classes for handling enumerations: Reflectionenum, ReflectenumunitCase, and ReflecteneNumbackedCase. There is also a new enum_exists function that is suggested by its name.
At this point, I believe you have a deeper understanding of the meaning and usage of "PHP enumeration". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.