Download the PHP package cruxinator/php-bitmask without Composer
On this page you can find all versions of the php package cruxinator/php-bitmask. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cruxinator/php-bitmask
More information about cruxinator/php-bitmask
Files in cruxinator/php-bitmask
Package php-bitmask
Short Description PHP Bitmask implementation
License MIT
Homepage https://github.com/cruxinator/php-bitmask
Informations about the package php-bitmask
PHP Bitmask implementation derived from myclabs/php-enum
Why?
First, and mainly, myclabs/php-enum
is restricted and can't be composited (Enum::FOO() | Enum::BAR() doesn't work too well).
Using a Bitmask instead of an integer provides the following advantages:
- You can type-hint:
function setAction(Bitmask $action) {
; - You can enrich the Bitmask with methods (e.g.
format
,parse
, …); - You can extend the Bitmask to add new values (make your enum
final
to prevent it); - You can get a list of all the possible values (see below);
- A Bitmask can many enums in a single value.
This Bitmask class is not intended to replace enums, but only to be used when it makes sense.
Installation
Declaration
Usage
As you can see, methods are automatically implemented to provide quick access to a Bitmask value.
One advantage over using class constants is to be able to type-hint Bitmap values:
Documentation
__construct()
The constructor checks that the value can be composed from the enum;__toString()
You canecho $myValue
, it will display the bitmask value (in an array style format of Boolean)getValue()
Returns the current value of the BitmaskgetKey()
Returns a key arrat of the current composite parts of the Bitmaskequals()
Tests whether Bitmask instances are equal (returnstrue
if enum values are equal,false
otherwise)
Static methods:
toArray()
Returns all possible values as an array (constant name in key, constant value in value)keys()
Returns the names (keys) of all constants in the Enum classvalues()
Returns instances of the Enum class of all Enum constants (constant name in key, Enum instance in value)isValid()
Check if tested value is valid on enum setisValidKey()
Check if tested key is valid on enum setsearch()
Return key for searched value
dynamic methods
Static method helpers are implemented using __callStatic()
and __call()
.
If you care about IDE autocompletion, you can either implement the static methods yourself:
Or you can use phpdoc (this is supported in PhpStorm for example):
Related projects
- [myclabs/php-enum] (https://github.com/myclabs/php-enum)