Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Dragonfly Research Group, LLC profile imageSenik Hakobyan
Senik Hakobyan forDragonfly Research Group, LLC

Posted on • Edited on

     

PHP Enum - get rid of boilerplate mappings

Hey there. Just decided to put together a small package, that will help me to get rid of boilerplate mappings likepublic function getAll(): array // returns some static array of strings.

You just need to define constants, and myEnum\Enum class will do the rest for you, thanks to Reflection API.

Here is an example:

<?phpnamespaceApp;useEnum\Enum;classStatusextendsEnum{constACTIVE='active';constINACTIVE='inactive';constBLOCKED='blocked';}$status=newStatus();$status->toArray();// will return [ 'active' => 'active', 'inactive' => 'inactive', 'blocked' => 'blocked' ]$status->hasValue('active');// will return boolean$status->hasKey('blocked');// will return boolean
Enter fullscreen modeExit fullscreen mode

Enum's constructor has 2 parameters:$caseLower=true and$caseUpper=false.

If you want the case of array keys to be same as constants case - passfalse as$caseLower.

$status=newStatus(false);
Enter fullscreen modeExit fullscreen mode

If you want the case of array keys to be uppercase - passfalse as$caseLower andtrue as$caseUpper.

$status=newStatus(false,true);
Enter fullscreen modeExit fullscreen mode

We just created our custom typeEnum. Now I can even use it to have better typed properties in my classes. For example:

<?phpnamespaceApp;useEnum\Enum;classPerson{publicint$identifier;publicstring$name;publicEnum$status;}
Enter fullscreen modeExit fullscreen mode

Now I know exactly what type has my$status and I know exactly how I should work with that type. I find this much better than using some raw arrays.

GitHub repo:

GitHub logo hakobyansen / phpenum

All constants in your class, which extends Enum, will be converted to array of enum options.

PHP Enum

This package provides you a class named Enum. It is designed to help you to structureyour Enum classes easily and get rid of the most boilerplate code.

Usage

Installation

Install via composer:

composer require codebot/phpenum

How to use

All your Enum classes should extend the abstractEnum class.All constants in your class, which extendsEnum, will be converted to enum options.

<?phpnamespaceApp;useEnum\Enum;classStatusextendsEnum{constACTIVE   ='active';constINACTIVE ='inactive';constBLOCKED  ='blocked';}$status =newStatus();$status->toArray();// will return [ 'active' => 'active', 'inactive' => 'inactive', 'blocked' => 'blocked' ]$status->hasValue('active');// will return boolean$status->hasKey('blocked');// will return boolean
Enter fullscreen modeExit fullscreen mode

Enum constructor has 2 parameters:$caseLower=true and…

Have a nice day!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

More fromDragonfly Research Group, LLC

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp