Skip to content

artkonekt/enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

a8e3057 · Mar 3, 2025
Mar 3, 2025
Feb 29, 2024
Feb 29, 2024
Feb 29, 2024
Mar 10, 2022
Mar 10, 2022
Sep 30, 2021
Oct 6, 2020
Feb 29, 2024
Mar 10, 2022
Feb 17, 2023
Mar 3, 2025
Mar 10, 2022

Repository files navigation

Konekt Enum

Tests 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.

Why not PHP 8.1 Enums? This package was created back in 2013, and has been in use across many libraries. We're planning to find the way to make this package be based on native PHP enums AND keep compatibility with earlier versions as much as possible. This is expected in the v5.0 release of this package.

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

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.