Perfect Enums in PHP -


recently came solution enums in php:

    class enum implements iterator {         private $vars = array();         private $keys = array();         private $currentposition = 0;          public function __construct() {         }          public function current() {             return $this->vars[$this->keys[$this->currentposition]];         }          public function key() {             return $this->keys[$this->currentposition];         }          public function next() {             $this->currentposition++;         }          public function rewind() {             $this->currentposition = 0;             $reflection = new reflectionclass(get_class($this));             $this->vars = $reflection->getconstants();             $this->keys = array_keys($this->vars);         }          public function valid() {             return $this->currentposition < count($this->vars);         }  } 

example:

class applicationmode extends enum {     const production = 'production';     const development = 'development'; }  class application {     public static function run(applicationmode $mode) {         if ($mode == applicationmode::production) {         //run application in production mode         }         elseif ($mode == applicationmode::development) {             //run application in development mode         }     } }  application::run(applicationmode::production); foreach (new applicationmode $mode) {     application::run($mode); } 

it works perfect, got ide hints, can iterate through enums think miss enums features can useful. question is: features can add make more use of enums or make more practical use?

i think can olso implement arrayaccess , countable

 class enum implements arrayaccess, countable, iterator { 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -