PHPackages                             macrominds/enum - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. macrominds/enum

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

macrominds/enum
===============

A simple enum implementation for php that allows for typehinting.

v1.1.0(8y ago)098MITPHPPHP ^7.0

Since Sep 9Pushed 8y ago1 watchersCompare

[ Source](https://github.com/macrominds/enum)[ Packagist](https://packagist.org/packages/macrominds/enum)[ RSS](/packages/macrominds-enum/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependencies (1)Versions (6)Used By (0)

A simple enum implementation for php that allows for typehinting.
=================================================================

[](#a-simple-enum-implementation-for-php-that-allows-for-typehinting)

[![Build Status](https://camo.githubusercontent.com/e35ffc560db44ddf8d7fee3269be1352906e4f198393f9a9288c9702b655793e/68747470733a2f2f7472617669732d63692e6f72672f6d6163726f6d696e64732f656e756d2e737667)](https://travis-ci.org/macrominds/enum)

Usage
-----

[](#usage)

### Example

[](#example)

In order to create a blazingly simple Salutation enum, just create it as follows:

```
use macrominds\enum\Enumerations;

class Salutation
{
	use Enumerations;
    protected static $enums = [
            'MRS' => 1,
            'MR' => 2,
            'MS' => 3
        ];
}

```

If you require complex epressions for your enum values, just take the alternative static method approach and you're fine.

```
//alternative approach

use macrominds\enum\Enumerations;

class AnyValueEnum
{
	use Enumerations;

    protected static function enums(){
    	return [
    		'String' => 'string',
            'Integer' => 2,
            'Object' => Salutation::MR()
        ];
    }
}

```

That's it. You are now able to typehint your functions and you're sure that you get instances of your custom enum. See tests/EnumTest.php if you're in doubt. It shows which expectations you can make.

```
// example for type hinted function
public function save(Salutation $salutation) {
	saveToDB($salutation->value());
}

// example for fetching the enum from value
public function load($value) {
    // throws \Exception if $value is invalid
    return Salutation::fromValue($value);
}

// example for fetching the enum from key
public function loadKey($key) {
    // throws \Exception if $key is invalid
    return Salutation::fromKey($key);
}

```

### IDE Support

[](#ide-support)

In order to support code completion, you may want to give hints about the resulting methods:

```
use macrominds\enum\Enumerations;

/**
 * @method static \macrominds\enum\Salutation MRS()
 * @method static \macrominds\enum\Salutation MR()
 * @method static \macrominds\enum\Salutation MS()
 */
class Salutation
{
    use Enumerations;
    protected static $enums = [
            'MRS' => 1,
            'MR' => 2,
            'MS' => 3
        ];
}

```

Installation
------------

[](#installation)

`composer require macrominds/enum`.

TODO
----

[](#todo)

- More source documentation.
- Make sure that configured names and values are unique: This is currently the developer's responsibility when creating a custom Enum.
- Make php5.6 compatible

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~151 days

Total

4

Last Release

3127d ago

Major Versions

v0.1.0 → v1.0.02016-09-25

### Community

Maintainers

![](https://www.gravatar.com/avatar/bad63aeaba5d382ba085e719837bd0c43311bc88323427edf39834b4e241ea81?d=identicon)[tpraxl](/maintainers/tpraxl)

---

Top Contributors

[![tpraxl](https://avatars.githubusercontent.com/u/913720?v=4)](https://github.com/tpraxl "tpraxl (42 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/macrominds-enum/health.svg)

```
[![Health](https://phpackages.com/badges/macrominds-enum/health.svg)](https://phpackages.com/packages/macrominds-enum)
```

###  Alternatives

[eonasdan/bootstrap-datetimepicker

Date/time picker widget based on twitter bootstrap

7.2k496.2k9](/packages/eonasdan-bootstrap-datetimepicker)[flexihash/flexihash

Flexihash is a small PHP library which implements consistent hashing

364719.5k5](/packages/flexihash-flexihash)[jbzoo/data

An extended version of the ArrayObject object for working with system settings or just for working with data arrays

851.6M23](/packages/jbzoo-data)[atrox/haikunator

Generate Heroku-like random names to use in your php applications.

110253.2k](/packages/atrox-haikunator)[dmstr/yii2-helpers

Yii2 Helpers

12640.0k5](/packages/dmstr-yii2-helpers)[yoast/yoast-seo-for-neos

Yoast SEO for Neos CMS

24169.3k](/packages/yoast-yoast-seo-for-neos)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
