[PHP] A custom ENUM

PHP has not enum, but there is a simple way to simulate it, using Class in php5.

I like to use this structure:

[code lang=”php” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]//
// example
//
class Outcode
{
const Ok     =  0;  // ok
const Old    = 40;  // very old db entry
const Bad    = 50; // bad input format
const DBKo   = 70; // db error / query null
const NoData = 80; // empty data
}[/code]

To use it:

[code lang=”php” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]$ret = Outcode::NoData;[/code]

enjoy.

 

Alberto Pasca

Software engineer @ Pirelli & C. S.p.A. with a strong passion for mobile  development, security, and connected things.