PHPackages                             tlr/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. tlr/enum

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

tlr/enum
========

An enum package optimised for laravel based on myclabs/enum

v3.0.0(4y ago)013.3k↓33.3%[1 PRs](https://github.com/tedslittlerobot/enum/pulls)MITPHPPHP ^8.0.0

Since Jan 4Pushed 4y agoCompare

[ Source](https://github.com/tedslittlerobot/enum)[ Packagist](https://packagist.org/packages/tlr/enum)[ RSS](/packages/tlr-enum/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (5)Versions (12)Used By (0)

TLR \\ PHP Enum Library
=======================

[](#tlr--php-enum-library)

[![Build Status](https://camo.githubusercontent.com/a8fadecbec3f3330d0bb0df01b62db24d2634fd31f0a5452293b06d4f62d9307/68747470733a2f2f7472617669732d63692e636f6d2f746564736c6974746c65726f626f742f656e756d2e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/tedslittlerobot/enum)

Deprecated
----------

[](#deprecated)

\*\* This package is deprecated as PHP 8.1 will support a native / primitive enum type, and solves 90% of the problems this package was meant to solve. \*\*

The source code will stay up here indefinitely, for anyone already using this library.

Introduction
------------

[](#introduction)

A raw PHP Enum / Flags library. Heavily based on [myclabs/php-enum](https://github.com/myclabs/php-enum) (I did not fork, as I made some subtle changes to the core classes).

With support for Laravel (and Nova).

Key features:

- Instantiatable, type-hintable enums.
- Flag Enum type (for help / usage with flags &amp; masks).

Key features for Laravel:

- Enum validation rule.
- Helpers to get displayable values out of enums.
- Getters and setters for eloquent usage.
- Easy Enum field for Laravel Nova.

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

[](#installation)

```
composer require tlr/enum
```

If you are on Laravel with package auto-detection, good for you, you're all set up. If not, add `Tlr\Phpnum\Laravel\EnumServiceProvider` to your loaded service providers.

What is an enum?
----------------

[](#what-is-an-enum)

An enumaration value - it is a value that represents one possible in a concrete list of values. It allows you to define a list of specific values, and be confident that your enum conforms to that list.

Consider the following function:

```
function createNewArticle(string $type) {
    // ...
}
```

Any string can be passed in to this function - 'article', 'recipe', 'monkeys', and while it may be easy to handle many possible types, you will always have to handle any types that don't exist.

An enum would allow you to type hint a value from a pre-determined list of values that are definied in your code, so you can be sure that you are dealing with a discrete (limited) choice of values (types, in this case). For example...

Basic Usage (Enums)
-------------------

[](#basic-usage-enums)

> Personally, I keep an application namespace specifically for all of enums in a project, and for the sake of these examples, I will be using the `App\Values` as the namespace for enums.

```
