drslump / Protobuf-PHP

PHP implementation of Google's Protocol Buffers with a protoc plugin compiler
http://drslump.github.com/Protobuf-PHP/
MIT License
462 stars 163 forks source link

Improve runtime for Enum types #5

Closed drslump closed 13 years ago

drslump commented 13 years ago

Right now Enums are generated as simple PHP classes with constants. They should inherit from a base Enum class that offers introspection features.

class Enum implements \Iterator, \ArrayAccess {             
    public function getInstance(){ 
        static $inst;
        return $inst ?: new static();
    }

    public function __isset() { ... }
    public function __get() { ... }
}

class MyEnum extends Enum {
   const FOO = 1;
   const BAR = 2;
}

$enum = MyEnum::getInstance();
foreach ($enum as $k=>$v) {
  echo $k => $v;
}

$enum['FOO'] === 1;
$enum[1] === 'FOO';
drslump commented 13 years ago

Implemented as DrSlump\Protobuf\Enum class which is inherited by generated enums.