Enums in PHP

12/3/2013 4:58:32 AM

Those familiar with Java and .NET are probably used to using enums. PHP does not natively support enums as a data type. To use something equivalent, I like to use classes with constant values. I usual put all of the enums together.

<?php

namespace Enums;

class Statuses
{
    const Enabled = 1;
    const Disabled = 2;
    const Approved = 3;
    const Rejected = 4;
    const Deleted = 5;
    // etc.
}


class Visibilities
{
    const Published = 1;
    const Unpublished = 2;
    // etc.
}


class ContentTypes
{
    const Article = 1;
    const Taxonomy = 2;
    const Image= 3;
    const Generic = 4;
    // etc.
}