Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create enumerations in PHP

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to create enumerations in PHP, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Enumerations in PHP

First, we will create a simple color enumeration that lists three colors: red, blue, and green. As with other languages, you probably know that we will refer to these color values in our code, such as Color.Red, Color.Blue, and Color.Green. We don't have to create some kind of instance or anything. Typically, a language defines its own enumerations as part of the language syntax and / or lets you add your own enumerations. PHP, on the other hand, does not have this concept. So what we have to do is use a class, an abstract class to be exact, to do what we want to do. For our various values, we will define some constants. This is what it looks like.

Abstract class Color {const RED = 1; const BLUE = 2; const GREEN = 3;}

With an abstract class, we do not need to create an instance of the class to access the variables it contains. With this definition, we can start using our enumeration class to compare values 1, 2, and 3, which are red, blue, and green, respectively. Why should we use enumerations in addition to readability? Well, maybe we can only store colors as integers in the database. We can store 1 in the database and then compare the value in the database to see if it is equal to Color.RED. Without this enumeration, we might not know that 1 is the representation of "red" in the database. Not only does it sound silly to use strings in a database, but you'll be surprised why things are never that simple. Especially when working in a large organization with dozens of developers.

How to use

Most people who are familiar with enumerations usually know how to use periods to access values. Such as Color.Blue or Color.Green. Because this is an abstract class, and the parsing operator that PHP uses to access class members is:: then you will have to use Color::BLUE and Color::GREEN. Not bad, huh?

Convert enumerated values to their keys

As mentioned at the beginning, most of the time you use an enumeration to compare some of the values you get from elsewhere with the values of the enumeration. Get the value from the database, compare it with Color.BLUE, and change the page color to blue in response. But what if we have a value like 3 and we want to know the colors in the enumeration? Of course, we can try this value for each of the three constants we define and get our answer. But what if we don't know what all the constant values are? What if there are 20 constants? It's a bit too much to make 20 if/else statements or to set up a switch with 20 case. The following code takes a different approach. We use reflection to get constants, traverse them, and compare the value of each constant with the value we provide. If it is not found, we return to null.

Function getConstantName ($enum, $constantValue) {try {$reflection = new ReflectionClass ($enum);} catch (ReflectionException $ex) {return null;} $constants = $reflection- > getConstants (); foreach ($constants as $key = > $val) {if ($val = $constantValue) {return $key;} return null;}

Using this code, we can enter the name of the Color enumeration and the value we are looking for, which will return the key. Make sure you give it an enumeration name or an instance of our class, and the function can see the definition! For example, if we call getConstantName ('Color', 3); it will return "green". You may or may not decide to use this feature, but if you want to know whether 3 is even equal to the color defined in our enumeration "Color", then you can certainly use it. If you use getConstantName ('Color', 6); you get a null value and know it's not even a supported color, and you can default the color of the page to Color::Red.

Future considerations

Now, of course, I know you might want to do a lot of other things with enumerations. And you can even do some things, such as mixing Color::BLUE and Color::YELLOW if you want it to return Color::GREEN. Because we have created an abstract class, there is no problem moving your function into the class and making it static. If you do some searches on the web, you will find that others have mentioned similar ideas and shifted them in other directions, including defining base classes and inheriting from them.

The above is all the content of the article "how to create enumerations in PHP". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report