{"id":15029295,"url":"https://github.com/dnl-blkv/simple-php-enum","last_synced_at":"2026-01-29T13:48:46.480Z","repository":{"id":56970473,"uuid":"86260954","full_name":"dnl-blkv/simple-php-enum","owner":"dnl-blkv","description":"Simple C/C++ alike enum library in PHP","archived":false,"fork":false,"pushed_at":"2017-04-14T12:06:48.000Z","size":105,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-09T04:02:32.468Z","etag":null,"topics":["c","c-plus-plus","enum","enumerated-types","enumeration","php","php-enum","php7","php71","simple"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/dnl-blkv/simple-php-enum","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dnl-blkv.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-26T19:59:22.000Z","updated_at":"2022-02-12T14:09:29.000Z","dependencies_parsed_at":"2022-08-21T06:40:24.190Z","dependency_job_id":null,"html_url":"https://github.com/dnl-blkv/simple-php-enum","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/dnl-blkv/simple-php-enum","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dnl-blkv%2Fsimple-php-enum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dnl-blkv%2Fsimple-php-enum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dnl-blkv%2Fsimple-php-enum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dnl-blkv%2Fsimple-php-enum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dnl-blkv","download_url":"https://codeload.github.com/dnl-blkv/simple-php-enum/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dnl-blkv%2Fsimple-php-enum/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28878762,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-29T10:31:27.438Z","status":"ssl_error","status_checked_at":"2026-01-29T10:31:01.017Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","c-plus-plus","enum","enumerated-types","enumeration","php","php-enum","php7","php71","simple"],"created_at":"2024-09-24T20:10:14.377Z","updated_at":"2026-01-29T13:48:46.459Z","avatar_url":"https://github.com/dnl-blkv.png","language":"PHP","readme":"# Simple PHP Enum\n## Description\nA simple C/C++ alike PHP library for Enums.\n\n## Stats and Health\n[![Travis CI](https://travis-ci.org/dnl-blkv/simple-php-enum.svg?branch=master)](https://travis-ci.org/dnl-blkv/simple-php-enum)\n[![codecov](https://codecov.io/gh/dnl-blkv/simple-php-enum/branch/master/graph/badge.svg)](https://codecov.io/gh/dnl-blkv/simple-php-enum)\n[![Latest Stable Version](https://poser.pugx.org/dnl-blkv/simple-php-enum/v/stable)](https://packagist.org/packages/dnl-blkv/simple-php-enum)\n[![License](https://poser.pugx.org/dnl-blkv/simple-php-enum/license)](https://packagist.org/packages/dnl-blkv/simple-php-enum)\n\n## PHP Version Support\nThe package is new and thus only supports PHP `\u003e=7.0`.\n\n# How To Basic\n## Installation\n1. [Install composer](https://getcomposer.org/doc/00-intro.md)\n2. Open your project folder in terminal\n3. Enter `composer require dnl-blkv/simple-php-enum`\n4. Wait for the composer to finish the job\n5. Now, you can start using the Simple PHP Enums as described below!\n\n## Defining Enums\nDefining a basic Enum with the package is straightforward:\n```\nuse dnl_blkv\\enum\\AbstractEnum;\n\n/**\n * @method static static CAT()\n * @method static static DOG()\n * @method static static BIRD()\n * @method static static FISH()\n */\nclass AnimalEnum extends AbstractEnum\n{\n    const CAT = null;\n    const DOG = null;\n    const BIRD = null;\n    const FISH = null;\n}\n```\n\nHere `null` means auto-determined ordinal value, or _auto-ordinal_. The default auto-ordinal is `0`. The further auto-ordinal values are determined as `{previous ordinal} + 1`.\n\nEnum constant names MUST be [PSR-1-compliant](http://www.php-fig.org/psr/psr-1/) AND start from a capital letter. If a constant name does not conform with the rules, the constant is ignored.\n\n## Creating\nOnce the class is defined, the enums can be acquired as:\n```\n$animal = AnimalEnum::CAT();\n```\n\nOr:\n```\n$animal = AnimalEnum::getByName('CAT');\n```\n\nOr:\n```\n$animal = AnimalEnum::getByOrdinal(0);\n```\n\nIn the examples above, if the name or the ordinal is not defined, exceptions will be thrown (`UndefinedEnumNameException` and `UndefinedEnumOrdinalException` correspondingly).\n\n## Accessing\nYou can access the name (string representation) and the ordinal (numeric representation) of the enum:\n```\necho $animal-\u003egetName() . ' ' . $animal-\u003egetOrdinal(); // Outputs \"CAT 0\"\n```\n\n## Comparison\n### Equality\nThe enums can be checked for equality as such:\n```\n$cat = AnimalEnum::CAT();\n$otherCat = AnimalEnum::CAT();\n$dog = AnimalEnum::DOG();\nvar_dump($cat-\u003eisEqual($otherCat)) // Outputs \"bool(true)\"\nvar_dump($cat-\u003eisEqual($dog)) // Outputs \"bool(false)\"\n```\nIntuitively, two enums of different types are never equal. If we have an enum of type `SomeOtherEnum` with `const VALUE = 0;` then the following holds:\n```\nvar_dump(SomeOtherEnum::VALUE()-\u003eisEqual(AnimalEnum::CAT())) // Outputs \"bool(false)\"\n```\n\n### Comparison by Ordinal\nIt is also possible to compare the Simple PHP Enums by their ordinal values. There are four methods defined for this, as shown below:\n```\n/**\n * @method static static READ()\n * @method static static WRITE()\n * @method static static ADMIN()\n */\nclass AccessLevelEnum extends Enum\n{\n    const READ = null;\n    const WRITE = null;\n    const ADMIN = null;\n}\n\nvar_dump(AccessLevelEnum::READ()-\u003eisLess(AccessLevelEnum::WRITE())) . PHP_EOL // -\u003e \"bool(true)\"\nvar_dump(AccessLevelEnum::READ()-\u003eisGreater(AccessLevelEnum::WRITE())) . PHP_EOL // -\u003e \"bool(false)\"\nvar_dump(AccessLevelEnum::READ()-\u003eisGreaterOrEqual(AccessLevelEnum::READ())) . PHP_EOL // -\u003e \"bool(true)\"\nvar_dump(AccessLevelEnum::READ()-\u003eisLessOrEqual(AccessLevelEnum::ADMIN())) . PHP_EOL // -\u003e \"bool(true)\"\n```\n\nIf two enums of different types are compared, the `InvalidArgumentException` is thrown.\n\n# How To Advanced\n## Defining Enums with Custom Ordinals\nBesides letting the library assign the ordinals automatically, you could manually assign custom integer values to the ordinals:\n```\nuse dnl_blkv\\enum\\AbstractEnum;\n\n/**\n * @method static static PIZZA()\n * @method static static SUSHI()\n * @method static static KEBAB()\n * @method static static SALAD()\n */\nclass FoodEnum extends AbstractEnum\n{\n    const PIZZA = 5;\n    const SUSHI = null;\n    const KEBAB = 8;\n    const SALAD = 10;\n}\n```\nIn this case the enums will be defined as following:\n```\necho FoodEnum::PIZZA()-\u003egetOrdinal() . PHP_EOL; // Outputs \"5\"\necho FoodEnum::SUSHI()-\u003egetOrdinal() . PHP_EOL; // Outputs \"6\"\necho FoodEnum::KEBAB()-\u003egetOrdinal() . PHP_EOL; // Outputs \"8\"\necho FoodEnum::SALAD()-\u003egetOrdinal() . PHP_EOL; // Outputs \"10\"\n```\n\n## Duplicate Ordinals\nSimilarly to the vanilla C/C++ enums, this Simple PHP Enums allow for duplicate ordinals. This may be used for tackling such cases as a default value:\n```\nuse dnl_blkv\\enum\\AbstractEnum;\n\n/**\n * @method static static LAGER()\n * @method static static IPA()\n * @method static static PORTER()\n * @method static static STOUT()\n * @method static static DEFAULT()\n * @method static static AFTER_DEFAULT()\n */\nclass BeerEnum extends AbstractEnum\n{\n    const LAGER = 0;\n    const IPA = null;\n    const PORTER = null;\n    const STOUT = null;\n    const DEFAULT = 0;\n    const AFTER_DEFAULT = null;\n}\n```\n\nFor the enum defined above, the following will hold:\n```\necho BeerEnum::DEFAULT()-\u003egetOrdinal() . PHP_EOL; // Outputs \"0\"\necho BeerEnum::AFTER_DEFAULT()-\u003egetOrdinal() . PHP_EOL; // Outputs \"1\"\n```\n\nIf you are getting an enum with duplicate ordinal using a magic method or by name, it works as usual.\n```\necho BeerEnum::DEFAULT()-\u003egetName() . PHP_EOL; // Outputs \"DEFAULT\"\necho BeerEnum::getByName('DEFAULT')-\u003egetName() . PHP_EOL; // Outputs \"DEFAULT\"\n```\n\nHowever, if you get it by an ordinal, the behavior is slightly different, and you have two options as shown below:\n```\necho BeerEnum::getFirstByOrdinal(0)-\u003egetName() . PHP_EOL; // Outputs \"LAGER\"\n$allEnumWithOrdinalZero = BeerEnum::getAllByOrdinal(0);\necho $allEnumWithOrdinalZero[0]-\u003egetName() . PHP_EOL; // Outputs \"LAGER\"\necho $allEnumWithOrdinalZero[1]-\u003egetName() . PHP_EOL; // Outputs \"DEFAULT\"\n```\n\n## More Equality\nThe Simple PHP Enum library only creates each enum object once and then reuses it. Therefore, the enums are comparable with `===` or its alias `isSame`. This kind comparison is stricter than `isEqual`. Whereas `isEqual` only accounts for the enum type and ordinal, `isSame` also takes the `name` into account:\n```\nvar_dump(BeerEnum::LAGER()-\u003eisEqual(BeerEnum::LAGER())); // Outputs \"bool(true)\"\nvar_dump(BeerEnum::LAGER()-\u003eisEqual(BeerEnum::DEFAULT())); // Outputs \"bool(true)\"\nvar_dump(BeerEnum::LAGER() === BeerEnum::LAGER()); // Outputs \"bool(true)\"\nvar_dump(BeerEnum::LAGER() === BeerEnum::DEFAULT()); // Outputs \"bool(false)\"\nvar_dump(BeerEnum::LAGER()-\u003eisSame(BeerEnum::LAGER())); // Outputs \"bool(true)\"\nvar_dump(BeerEnum::LAGER()-\u003eisSame(BeerEnum::DEFAULT())); // Outputs \"bool(false)\"\n```\n\n## Checking Existence of Names and Ordinals\nIf you wish to check whether or not certain enum type has a given name or ordinal, there are methods allowing you to easily do so:\n```\nvar_dump(BeerEnum::isNameDefined('STOUT')) // Outputs \"bool(true)\";\nvar_dump(BeerEnum::isNameDefined('VODKA')) // Outputs \"bool(false)\";\nvar_dump(BeerEnum::isOrdinalDefined(3)) // Outputs \"bool(true)\";\nvar_dump(BeerEnum::isOrdinalDefined(420)) // Outputs \"bool(false)\";\n```\n\n## Converting to String\nThe enums have an embedded magical mechanism for serialization:\n```\necho BeerEnum::IPA() . PHP_EOL;\n\n/* \n * Outputs:\n * {\n *     \"\\your\\name\\space\\BeerEnum\": {\n *         \"name\": \"IPA\",\n *         \"ordinal\": 1\n *      }\n * }\n */\n```\n\n# Notes\n## Extension\nAll the internals of the `AbstractEnum` class are either `public` or `protected`. Therefore, it is completely open for extension and allows you to build your own, more complex constructions on top of it.\n\n## Use with Databases\nIf you opt to use these enums with databases and store the ordinals, I would recommend to make sure that no stored enum has duplicate ordinals. Otherwise, it could happen that you store `DEFAULT = 0`, but receive `IPA = 0` upon recreation.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdnl-blkv%2Fsimple-php-enum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdnl-blkv%2Fsimple-php-enum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdnl-blkv%2Fsimple-php-enum/lists"}