Enumeration - In PHP, we use a large number of numbers to identify types or states. What is the best solution?

WBOY
Release: 2016-09-05 08:59:52
Original
1140 people have browsed it

Now we use an ftype field int type in the database table in MySQL to identify the type. At this time, there are about 20 types. Because of its age, the various numbers in the program look very laborious.

How do you deal with this kind of problem in the program?

Now our thinking is:

  1. Restrict programmers to use numbers to add labels privately, add a class such as:

<code>class action
{
    const EDIT = 0;
    const VIEW = 1;
    //...
}</code>
Copy after login
Copy after login

You must use parameter action to use

2. Use the open source enumeration class on github to implement
such as: https://github.com/myclabs/ph...

Reply content:

Now we use an ftype field int type in the database table in MySQL to identify the type. At this time, there are about 20 types. Because of its age, the various numbers in the program look very laborious.

How do you deal with this kind of problem in the program?

Now our thinking is:

  1. Restrict programmers to use numbers to add labels privately, add a class such as:

<code>class action
{
    const EDIT = 0;
    const VIEW = 1;
    //...
}</code>
Copy after login
Copy after login

You must use parameter action to use

2. Use the open source enumeration class on github to implement
such as: https://github.com/myclabs/ph...

The enumeration class is a good choice, especially when the entire project tends to be object-oriented.

The two methods you listed are actually similar, except that the enumeration class provides richer functions.
Just choose according to your actual needs.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template