Tag Archives: Wishful Thinking

Pseudo-ENUM in PHP

I’ve been working on a project lately where I’m rolling my own basic role-based access control (RBAC) system. In experimenting with different ways to handle the role definitions, I though that it’d be nice to have a basic enum() function or construct, as a quick, convenient way to define a set of constants. There’s this option:

<?php
    define('ADMIN',     0);
    define('MODERATOR', 1);
    define('EDITOR',    2);

but that isn’t exactly succinct. I’d rather have something like this:

<?php
    enum('ADMIN', 'MODERATOR', 'EDITOR');
    // ADMIN == 0 
    // MODERATOR == 1 
    // etc.

Continue reading