{"id":19218940,"url":"https://github.com/marc-mabe/php-enum-cl","last_synced_at":"2025-05-13T00:27:23.792Z","repository":{"id":47295254,"uuid":"350841891","full_name":"marc-mabe/php-enum-cl","owner":"marc-mabe","description":"Compatibility layer for emulating enumerations in PHP \u003c 8.1 and use native enumerations in PHP \u003e= 8.1","archived":false,"fork":false,"pushed_at":"2022-01-18T17:16:53.000Z","size":165,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-01T05:02:11.790Z","etag":null,"topics":["compatibility","enum","php","polyfill"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/marc-mabe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-23T20:02:57.000Z","updated_at":"2025-02-24T10:59:14.000Z","dependencies_parsed_at":"2022-09-01T08:40:18.475Z","dependency_job_id":null,"html_url":"https://github.com/marc-mabe/php-enum-cl","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marc-mabe%2Fphp-enum-cl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marc-mabe%2Fphp-enum-cl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marc-mabe%2Fphp-enum-cl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marc-mabe%2Fphp-enum-cl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marc-mabe","download_url":"https://codeload.github.com/marc-mabe/php-enum-cl/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249958686,"owners_count":21351699,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["compatibility","enum","php","polyfill"],"created_at":"2024-11-09T14:28:49.307Z","updated_at":"2025-04-20T20:32:11.639Z","avatar_url":"https://github.com/marc-mabe.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Compatibility layer for emulating enumerations in PHP \\\u003c 8.1 but native enumerations in PHP \\\u003e= 8.1\n\n[![Build Status](https://github.com/marc-mabe/php-enum-cl/workflows/Test/badge.svg?branch=main)](https://github.com/marc-mabe/php-enum-cl/actions?query=workflow%3ATest%20branch%3Amain)\n[![Code Coverage](https://codecov.io/github/marc-mabe/php-enum-cl/coverage.svg?branch=main)](https://codecov.io/gh/marc-mabe/php-enum-cl/branch/main/)\n\n## How-to create\n\n**Vendor\\MyEnum.php**\n```php\n\u003c?php declare(strict_types=1);\n\nif (PHP_VERSION_ID \u003c 80100) {\n    require_once __DIR__ . '/MyEnum-emulated.php';\n} else {\n    require_once __DIR__ . '/MyEnum-native.php';\n}\n```\n\n**Vendor\\MyEnum-emulated.php**\n\n```php\n\u003c?php declare(strict_types=1);\n\nnamespace Vendor;\n\nuse Mabe\\Enum\\Cl\\EmulatedIntEnum;\n\nfinal class MyEnum extends EmulatedIntEnum\n{\n    protected const ZERO = 0;\n    protected const ONE = 1;\n    protected const TWO = 2;\n    protected const THREE = 3;\n    protected const FOUR = 4;\n    protected const FIVE = 5;\n    protected const SIX = 6;\n    protected const SEVEN = 7;\n    protected const EIGHT = 8;\n    protected const NINE = 9;\n}\n```\n\n**Vendor\\MyEnum-native.php**\n```php\n\u003c?php declare(strict_types=1);\n\nnamespace Vendor;\n\nuse Mabe\\Enum\\Cl\\EnumBc;\n\nenum MyEnum:int\n{\n    use EnumBc;\n\n    case ZERO = 0;\n    case ONE = 1;\n    case TWO = 2;\n    case THREE = 3;\n    case FOUR = 4;\n    case FIVE = 5;\n    case SIX = 6;\n    case SEVEN = 7;\n    case EIGHT = 8;\n    case NINE = 9;\n}\n```\n\n| Enum type           | native                                                                                                                    | emulated                                                                                                                                      |\n|---------------------|---------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------|\n| Unit enum           | \u003cpre\u003eenum ENUMNAME {\u003cbr\u003e    use \\Mabe\\Enum\\Cl\\EnumBc;\u003cbr\u003e    case CASENAME;\u003cbr\u003e    // ...\u003cbr\u003e}\u003c/pre\u003e                      | \u003cpre\u003efinal class ENUMNAME extends \\Mabe\\Enum\\Cl\\EmulatedUnitEnum {\u003cbr\u003e    protected const CASENAME = null;\u003cbr\u003e    // ...\u003cbr\u003e}\u003c/pre\u003e             |\n| Integer backed enum | \u003cpre\u003eenum ENUMNAME:int {\u003cbr\u003e    use \\Mabe\\Enum\\Cl\\EnumBc;\u003cbr\u003e    case CASENAME = CASEVALUE;\u003cbr\u003e    // ...\u003cbr\u003e}\u003c/pre\u003e      | \u003cpre\u003efinal class ENUMNAME extends \\Mabe\\Enum\\Cl\\EmulatedIntEnum {\u003cbr\u003e    protected const CASENAME = CASEVALUE;\u003cbr\u003e    // ...\u003cbr\u003e}\u003c/pre\u003e         |\n| String backed enum  | \u003cpre\u003eenum ENUMNAME:string {\u003cbr\u003e    use \\Mabe\\Enum\\Cl\\EnumBc;\u003cbr\u003e    case CASENAME = 'CASEVALUE';\u003cbr\u003e    // ...\u003cbr\u003e}\u003c/pre\u003e | \u003cpre\u003efinal class ENUMNAME extends \\Mabe\\Enum\\Cl\\EmulatedStringEnum {\u003cbr\u003e    protected const CASENAME = 'CASEVALUE';\u003cbr\u003e    // ...\u003cbr\u003e}\u003c/pre\u003e    |\n\nFor IDE and static code analyzers I recommend adding the following docblock:\n\n```php\n/**\n * @method static self CASENAME()\n */\n```\n\n## How-to use\n\nThe following will work the same on PHP\u003c8.1 (using emulated enums)\nand on PHP\u003e=8.1 (using native enums):\n\n```php\n\u003c?php declare(strict_types=1);\n\nnamespace Vendor;\n\nuse function Mabe\\Enum\\Cl\\enum_exists;\n\n$zero = MyEnum::ZERO();\n$zero = MyEnum::from(0);\n$zero = MyEnum::tryFrom(0);\n$cases = MyEnum::cases();\n\n$zero-\u003evalue; // 0\n$zero-\u003ename;  // ZERO\n\n$zero instanceof \\UnitEnum;   // true\n$zero instanceof \\BackedEnum; // true\n\nMyEnum::ZERO() === MyEnum::from(0);     // true\nMyEnum::from(0) === MyEnum::tryFrom(0); // true\n\nenum_exists(MyEnum::class); // true\nenum_exists('stdClass');    // false\n```\n\n**Warning:** The following will **not** behave the same on all PHP versions:\n```php\n\u003c?php declare(strict_types=1);\n\nnamespace Vendor;\n\nMyEnum::ZERO; // PHP\u003c8.1:  Error: Cannot access protected const MyEnum::ZERO\n              // PHP\u003e=8.1: enum(MyEnum::ZERO)\n\nserialize(MyEnum::ZERO()); // PHP\u003c8.1:  Error: Trying to serialize a non serializable emulated enum case of MyEnum\n                           // PHP\u003e=8.1: \"E:11:\"MyEnum:ZERO\"\n```\n\n## Additional Notes\n\n### PHPStan\n\nBy default PHPStan will complain about unused constants\nas it can't automatically detect the special use via reflection in this case.\n\nTo avoid this you need to add the following to your `phpstan.neon[.dist]`:\n\n```\nservices:\n    -\n        class: Mabe\\Enum\\Cl\\PHPStan\\ConstantExtension\n        tags:\n            - phpstan.constants.alwaysUsedClassConstantsExtension\n```\n\nFor more information please read https://phpstan.org/developing-extensions/always-used-class-constants\n\n\n### Included Polyfills\n\nThis library includes the following polyfills (if not already present):\n\n* `get_debug_type`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarc-mabe%2Fphp-enum-cl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarc-mabe%2Fphp-enum-cl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarc-mabe%2Fphp-enum-cl/lists"}