Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rikudousage/php-enums

Yet another implementation of enums in php
https://github.com/rikudousage/php-enums

Last synced: about 1 month ago
JSON representation

Yet another implementation of enums in php

Awesome Lists containing this project

README

        

# PHP Enums

This is yet another PHP enum implementation.

What is so different about this one? It uses trait which solves one problem all the
other implementations I've seen have - extending some base enum class.

All the enums return the same instance for the same enum, so you can check for equality
(`===`) and the result is true.

The constructor is made private, so you cannot directly construct an instance via
`new` keyword (but you can create an constructor in your class and make it public
if you need to, but I advise against it).

Every internal method is made private so you cannot extend the enum (well, you can
but it would be useless).

```php
getValue(); // echoes "SomeCoolValue"
```

The methods you create have a value that you give them:

```php
getValue(); // echoes 1
```

## Some caveats

- the trait caches the objects based on value, meaning that if you create
two enum values (static methods) with same value, they will return the same object,
e.g. they will return true for equality test

```php