konekt/enum

SPL inspired PHP enum class implementation

3.1.0 2020-11-28 21:37 UTC

This package is auto-updated.

Last update: 2020-11-28 22:30:12 UTC


README

Travis Build Status Packagist Stable Version Packagist downloads StyleCI MIT Software License

PHP Enum Class

Enums are handy when a variable (especially a method parameter) can only take one out of a small set of possible values.

Konekt Enum is a lightweight abstract class that enables creation of PHP enums.

Usage

Extend the base class and define constants on it:

Example
class ChessPiece extends \Konekt\Enum\Enum {
    const KING   = 'king';
    const QUEEN  = 'queen';
    const ROOK   = 'rook';
    const BISHOP = 'bishop';
    const KNIGHT = 'knight';
    const PAWN   = 'pawn';
}

var $queen = new ChessPiece('queen');

Installation

using composer: composer require konekt/enum

Documentation

For detailed usage and examples go to the Konekt Enum Documentation or refer to the markdown files in the docs/ folder of this repo.

For the list of changes read the Changelog.

Upgrade

From 1.x -> 2.x see Upgrade to 2.0

From 2.x -> 3.x see Upgrade to 3.0

Laravel Eloquent Integration

There is a tiny trait for Laravel that helps you to automatically map fields of Eloquent models to/from Enum objects. For more details go to the konekt/enum-eloquent package.