{"id":23709918,"url":"https://github.com/nxtlvlsoftware/php-enums","last_synced_at":"2025-09-03T16:32:11.506Z","repository":{"id":57029488,"uuid":"171682142","full_name":"NxtLvLSoftware/php-enums","owner":"NxtLvLSoftware","description":"Brings enumerations to PHP!","archived":false,"fork":false,"pushed_at":"2019-03-02T14:37:54.000Z","size":37,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2023-08-07T10:08:32.831Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NxtLvLSoftware.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":"2019-02-20T13:53:23.000Z","updated_at":"2019-03-02T14:37:55.000Z","dependencies_parsed_at":"2022-08-23T18:50:22.642Z","dependency_job_id":null,"html_url":"https://github.com/NxtLvLSoftware/php-enums","commit_stats":null,"previous_names":[],"tags_count":0,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NxtLvLSoftware%2Fphp-enums","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NxtLvLSoftware%2Fphp-enums/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NxtLvLSoftware%2Fphp-enums/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NxtLvLSoftware%2Fphp-enums/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NxtLvLSoftware","download_url":"https://codeload.github.com/NxtLvLSoftware/php-enums/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231902080,"owners_count":18443330,"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":[],"created_at":"2024-12-30T18:58:55.009Z","updated_at":"2024-12-30T18:58:55.638Z","avatar_url":"https://github.com/NxtLvLSoftware.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Enumerations for PHP\n===============\n_Brings class-based enums to PHP!_\n\n[![Build Status](https://travis-ci.org/NxtLvLSoftware/php-enums.svg?branch=master)](https://travis-ci.org/NxtLvLSoftware/php-enums)\n\n### About\n\nThis package provides you with a quick and easy way to implement enumerations in your PHP codebase. The purpose of this\npackage is to make defining enums quick and do it with as least code as possible. This is achieved by reading constants\nfrom a class providing and then providing access to them through magic static methods that have the same name as the constant.\nWe use this approach to wrap the underlying constant value inside an immutable object that remains the same throughout the\ncurrent runtime. Using an object rather than the underlying value of the constant helps to replicate the behaviour of enums\nin other programming languages (an enum is not treated as an int even through deep down it is) and aims to deter the reliance\non 'magic numbers' (constant values which rarely change but are not guaranteed to remain the same) by hiding the underlying value.\n\nHere's a quick example:\n```php\nfinal class MyEnum extends nxtlvlsoftware\\enums\\Enum {\n    protected const ENUM_INT = 0;\n    protected const ENUM_STRING = \"string\";\n}\n```\n\nWe can then access these declared enums by calling a static method on the class with the equivalent name:\n\n```php\nMyEnum::ENUM_INT();\nMyEnum::ENUM_STRING();\n```\n\nWe use protected constants in this example to hide them from other classes, we only want them accessed through the static\nmethod. The enum class is also declared as final to stop other other classes extending it.\n\n### Installation\n\nAll you have to do to install with composer is the following:\n\n```bash\n$ composer require nxtlvlsoftware/enums\n```\n\nOr add it directly to your composer.json manifest:\n\n```json\n{\n    \"require\": {\n        \"nxtlvlsoftware/enums\": \"*\"\n    }\n}\n```\n\nThe library will start working out of the box, just make sure you extend the base enum class or the static methods won't\nbe defined on your class.\n\n### IDE Auto-completion\n\nYou may be thinking to yourself `Cool, but my IDE won't know these 'enums' even exist!` and you'd be right.\nAn IDE such as PhpStorm will have no idea these magic methods exist which is why we provide an 'IDE helper generator'.\nIt's a simple CLI application that will scan a directory for enum classes and generate a file [like this](https://gist.github.com/JackNoordhuis/a73dbf5cd32dac4ce44ad9177add3816)\nthat will tell your IDE that these methods actually will exist when the code runs.\n\nIf you require this package globally with composer the command will be available anywhere on your machine as `enums` or any\nproject which has this package as a dependency will link the script into the projects bin directory (`vendor/bin/enums` by default).\n\nYou will have to define at least one directory to scan and the location and filename of the stubs:\n```bash\n$ php vendor/bin/enums generate --dir /Users/Jack/Projects/MyProject/src --dir /Users/Jack/Projects/MyProject/vendor --out /Users/Jack/Projects/MyProject/enum_stubs.php\n```\n\nThis command will scan the source directory of the project and all of it's installed composer dependencies and generate\nthe IDE helper stubs in the `enum_stubs.php` file.\n\nYou can use the shortcut option names `-d` for specifying a directory and `-o` for the output file instead of the full names.\nCreating a simple script or PhpStorm configuration to quickly run this command will probably improve your workflow as\ntyping the whole command out every time you update one of your enum classes will get very tiresome.\n\n### Under the hood\n\nThe first time an enum is accessed on a class the library uses reflection to fetch all the constants defined, it then stores\na new instance of the class (in an array) which internally stores the enum value and name. This allows us to implement the\nmagic to string method to display the name of the enum for debugging purposes. The value is stored for potential a future\nfeature that will allow an underlying value to be returned in place of the enum object for special use-cases.\n\n### Issues\n\nFound a problem with this library? Make sure to open an issue on the [issue tracker](https://github.com/NxtLvLSoftware/php-enums/issues) and we'll get it sorted!\n\n#\n\n__The content of this repo is licensed under the Unlicense. A full copy of the license is available [here](LICENSE).__","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnxtlvlsoftware%2Fphp-enums","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnxtlvlsoftware%2Fphp-enums","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnxtlvlsoftware%2Fphp-enums/lists"}