Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bonndan/phpenums
https://github.com/bonndan/phpenums
enum php7
Last synced: 4 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/bonndan/phpenums
- Owner: bonndan
- License: mit
- Created: 2017-01-27T21:56:46.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T17:27:00.000Z (about 1 year ago)
- Last Synced: 2024-04-16T02:53:12.771Z (9 months ago)
- Topics: enum, php7
- Language: PHP
- Size: 10.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: license.txt
Awesome Lists containing this project
README
# PHP Enums
[![Build Status](https://travis-ci.org/bonndan/phpenums.svg?branch=master)](https://travis-ci.org/bonndan/phpenums)
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%207.0-8892BF.svg)](https://php.net/)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:```php
class Fruit extends \Pava\Enum
{
/**
* @var Fruit
*/
public static $APPLE;/**
* @var Fruit
*/
public static $BANANA;
}```
## So you can use static typing:
```php
$mix = function (Fruit $a, Fruit $b) {};
$blend = $mix(Fruit::$APPLE, Fruit::$BANANA);
$grow = function () : Fruit { return Fruit::$APPLE };
assert(Fruit::$APPLE instanceof Fruit);
```
## What are the drawbacks?
* Each enum has to be registered (unless static constructors arrive in PHP). The reason is that static properties can only have scalar values as default.
```php
//class Fruit ...
Pava\register(Fruit::class);
```* You need to annotate all static props in order to get your IDE working with it, but at least this works.
* No default values usable. At "compile time" $BANANA is not yet an instance. However you can use null as default value.```php
//not possible
$a = function (Fruit $a = Fruit::$BANANA) {};```
* Use/import is not possible, i.e. you cannot just write $BANANA.
## Advanced use
Each enum instance can obtain its own properties by implementing the magic invoke method.
```php
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();
```