dev-master
9999999-devAn experimental Enum implementation for PHP7
MIT
The Requires
- php ^7
The Development Requires
0.1.0
0.1.0.0An experimental Enum implementation for PHP7
The Requires
- php ^7
The Development Requires
An experimental Enum implementation for PHP7
Yet another implementation of Enums in PHP. The difference to most other implementations is that the implemetation is very close to Java. Static properties are used, but like in Java they are real instances of the Enum providing all methods etc:, (*2)
class Fruit extends \Pava\Enum { /** * @var Fruit */ public static $APPLE; /** * @var Fruit */ public static $BANANA; }
$mix = function (Fruit $a, Fruit $b) {}; $blend = $mix(Fruit::$APPLE, Fruit::$BANANA); $grow = function () : Fruit { return Fruit::$APPLE }; assert(Fruit::$APPLE instanceof Fruit);
//class Fruit ... Pava\register(Fruit::class);
//not possible $a = function (Fruit $a = Fruit::$BANANA) {};
Each enum instance can obtain its own properties by implementing the magic invoke method., (*3)
class Fruit extends \Pava\Enum { private $color; /** * @var Fruit */ public static $APPLE; /** * @var Fruit */ public static $BANANA; public function color() : string { return $this->color; } public function __invoke() { if ($this->name() == 'BANANA') $this->color = 'yellow'; if ($this->name() == 'APPLE') $this->color = 'green'; } } Pava\register(Fruit::class); echo 'The apple is ' . Fruit::$APPLE->color();
An experimental Enum implementation for PHP7
MIT
An experimental Enum implementation for PHP7