Download the PHP package henrywhitaker3/bitmasked-properties without Composer
On this page you can find all versions of the php package henrywhitaker3/bitmasked-properties. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download henrywhitaker3/bitmasked-properties
More information about henrywhitaker3/bitmasked-properties
Files in henrywhitaker3/bitmasked-properties
Package bitmasked-properties
Short Description A package to use bitmasked properties on classes
License MIT
Informations about the package bitmasked-properties
Bitmasked Properties
This library provides a trait to use bitmasked properties with PHP 8.1 enums. I was reading this and could think a few different places where this would be useful for me, so I decided to make this library to make it a bit more re-usable; it also felt like a good way to start playing around with PHP 8.1 enums.
Installation
Usage
For example, you have a model Person
that can be opted in or out for email and sms comms. A simple (not v good) way of doing this could be having individual columns in the database to store each of these methods:
This requires hard-coding the field names and having to run migrations to add columns when a new communication type comes along, which is a bit gross.
For some scenarios, using a bitmasked field would be a far nicer solution - only some as you can't index these values and therefore can't query them very efficiently. But, it allows you to just add a new case to the enum whenever a new communication type gets added with no change to the database. Using this you can have up to 32 different boolean values in a standard integer field. Here's how to use it for the person example above:
Now it's really simple to use:
You can add a new value to the Optin
enum with no changes to the database or code.