Home >Backend Development >PHP Problem >How to implement JAVA enumeration function in PHP

How to implement JAVA enumeration function in PHP

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-06-18 17:18:401416browse

This article will introduce to you how to implement JAVA enumeration function in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to implement JAVA enumeration function in PHP

How to implement JAVA enumeration function in PHP

Overview

In iteration After N versions, the latest version has finally implemented the PHP enumeration library php-enum

which is very similar to the definition and use of Java enumerations. It is necessary to mention here why Java enumerations must be implemented. Function. I got to know enumerations in Java. Before that, I had used PHP for a long time and had never heard of enumerations. However, in Java projects, enumerations can be seen everywhere, especially when the API returns a unified status code. In the scene, it has almost become the norm, so I have no way to ignore it, so I also learn to use it. When I use PHP again, I find that I am not used to not having enumerations, so I feel like searching for PHP enumerations

As we all know, there are two options for using enumerations in PHP. One is the official enumeration library provided in SPL. Are you particularly happy to see this? Don't worry, after reading the documentation you will find that not only do you have to install it as an extension, but the methods it provides are also very limited. So usually we choose the second method, which is to use a third-party enumeration library. By reading the source code of third-party enumerations, you will also find that they more or less have the shadow of Java enumerations. But if they had implemented the functions of Java enumerations, there would not be today's libraries and articles.

I checked a lot of enumerations and found that they all lack the core functionality of Java enumerations, which is custom attribute values ​​(in Java enumerations, enumerations are not simply defining constant names and constant values , you can define attributes to carry elements in the enumeration) At the same time, I also found that it is not easy to implement this function in PHP, so I wrote an abstract class in the project to achieve the function of fixing two attributes, because it requires multiple After using it in the project, I put it on github. After many reconstructions and overturning my own ideas again and again, I finally got close to the implementation of Java enumeration

Installation

composer require phpenum/phpenum

Quick Start

PHPEnum is very similar to Java enumerations, such as defining an enumeration that represents gender

In Java:

public enum GenderEnum {
    MALE(1, "male"),
    FEMALE(2, "female");

    private Integer id;
    private String name;

    GenderEnum(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}

Using PhpEnum:

class GenderEnum extends \PhpEnum\Enum
{
    const MALE = [1, 'male'];
    const FEMALE = [2, 'female'];

    private $id;
    private $name;

    protected function construct($id, $name)
    {
        $this->id = $id;
        $this->name->$name;
    }

    public function getId()
    {
        return $this->id;
    }
    
    public function getName()
    {
        return $this->name;
    }
}

You will find that their usage is also very similar

In Java:

GenderEnum.values(); // enum instance array
GenderEnum.valueOf("FEMALE"); // enum instance
GenderEnum.MALE.equals(GenderEnum.valueOf("MALE")); // true
GenderEnum.MALE.name(); // MALE
GenderEnum.MALE.ordinal(); // 0
GenderEnum.MALE.toString(); // MALE
GenderEnum.MALE.getId(); // 1
GenderEnum.MALE.getName(); // male

Using PhpEnum:

GenderEnum::values(); // enum instance array
GenderEnum::valueOf('FEMALE'); // enum instance
GenderEnum::MALE()->equals(GenderEnum::valueOf('MALE')); // true
GenderEnum::MALE()->name(); // MALE
GenderEnum::MALE()->ordinal(); // 0
(string)GenderEnum::MALE(); // MALE
GenderEnum::MALE()->getId(); // 1
GenderEnum::MALE()->getName(); // male

Not only In this way, PhpEnum also provides advanced functions in subclasses

GenderEnum::MALE()->idEquals(1); // true
GenderEnum::MALE()->NameEquals('male'); // true
GenderEnum::containsId(1); // 1
GenderEnum::containsName('male'); // 1
GenderEnum::ofId(1); // enum instance
GenderEnum::ofName('male'); // enum instance

Recommended learning: php video tutorial

The above is the detailed content of How to implement JAVA enumeration function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete