PHPackages                             cruxinator/php-bitmask - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. cruxinator/php-bitmask

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

cruxinator/php-bitmask
======================

PHP Bitmask implementation

0.2.0(2y ago)124.2k↓27.3%1[1 PRs](https://github.com/cruxinator/php-bitmask/pulls)1MITPHPPHP &gt;=7.1 | &gt;=8.0CI passing

Since Jan 16Pushed 3mo ago2 watchersCompare

[ Source](https://github.com/cruxinator/php-bitmask)[ Packagist](https://packagist.org/packages/cruxinator/php-bitmask)[ Docs](https://github.com/cruxinator/php-bitmask)[ RSS](/packages/cruxinator-php-bitmask/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (7)Used By (1)

PHP Bitmask implementation derived from `myclabs/php-enum`
==========================================================

[](#php-bitmask-implementation-derived-from-myclabsphp-enum)

[![Build Status](https://camo.githubusercontent.com/8e9fcb51c03287f9bf71e8210165afebeda9503b0d04fa34d97ba302f3a93f23/68747470733a2f2f7472617669732d63692e6f72672f63727578696e61746f722f7068702d6269746d61736b2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/cruxinator/php-bitmask)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/5332022516ad24ff1816efff9fd85f03765449eca140e5a380ba5336671fb2e3/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f63727578696e61746f722f7068702d6269746d61736b2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/cruxinator/php-bitmask/?branch=master)[![Coverage Status](https://camo.githubusercontent.com/1b635e049bf51e249490b595f933787b0022e99c707089e1c0a4503e4dbc1a54/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f63727578696e61746f722f7068702d6269746d61736b2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/cruxinator/php-bitmask?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/453ca7da140a3f25050ce001beef95444305b543387ea189d312402d6e34f285/68747470733a2f2f706f7365722e707567782e6f72672f63727578696e61746f722f7068702d6269746d61736b2f762f737461626c65)](https://packagist.org/packages/cruxinator/php-bitmask)[![Latest Unstable Version](https://camo.githubusercontent.com/f6312709ea76113e89f0f43c15c26d94cc8fc3c7becaa0fbaa155e29597d7325/68747470733a2f2f706f7365722e707567782e6f72672f63727578696e61746f722f7068702d6269746d61736b2f762f756e737461626c65)](https://packagist.org/packages/cruxinator/php-bitmask)[![Total Downloads](https://camo.githubusercontent.com/8b48b1c7b5c25c9f54ef42b194f37555d9a73cd2bb5818ae59b2f0b6ef505c32/68747470733a2f2f706f7365722e707567782e6f72672f63727578696e61746f722f7068702d6269746d61736b2f646f776e6c6f616473)](https://packagist.org/packages/cruxinator/php-bitmask)[![Monthly Downloads](https://camo.githubusercontent.com/56fafe46df3dce5de13cc82e44a808ba8fb984ee02ec31fb5fce852c432de23a/68747470733a2f2f706f7365722e707567782e6f72672f63727578696e61746f722f7068702d6269746d61736b2f642f6d6f6e74686c79)](https://packagist.org/packages/cruxinator/php-bitmask)[![Daily Downloads](https://camo.githubusercontent.com/8f2b7925ac7013d19b018e4e766ae255def63af115a61b3fbff03e9d595ff1e2/68747470733a2f2f706f7365722e707567782e6f72672f63727578696e61746f722f7068702d6269746d61736b2f642f6461696c79)](https://packagist.org/packages/cruxinator/php-bitmask)

Why?
----

[](#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
------------

[](#installation)

```
composer require cruxinator/php-bitmask

```

Declaration
-----------

[](#declaration)

```
use Cruxinator\BitMask\BitMask ;

class UserStatus extends BitMask
{

const Registered = 1; // BIT #1 of $flags has the value 1

const Active = 2; // BIT #2 of $flags has the value 2

const Member = 4; // BIT #3 of $flags has the value 4

const Admin = 8; // BIT #4 of $flags has the value 8

}
```

Usage
-----

[](#usage)

```
$status = UserStatus::Registered();

// or with a dynamic key:
$status = UserStatus::$key();
// or with a dynamic value:
$status = new UserStatus($value);

// values can then be checked
if ($status->isActive()){
// ...
}

// individuals flags can later toggled ON
$status->setActive(true);
// or off
$status->setActive(false);
```

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:

```
function setStatus(UserStatus $action) {
    // ...
}
```

Documentation
-------------

[](#documentation)

- `__construct()` The constructor checks that the value can be composed from the enum;
- `__toString()` You can `echo $myValue`, it will display the bitmask value (in an array style format of Boolean)
- `getValue()` Returns the current value of the Bitmask
- `getKey()` Returns a key arrat of the current composite parts of the Bitmask
- `equals()` Tests whether Bitmask instances are equal (returns `true` 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 class
- `values()` 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 set
- `isValidKey()` Check if tested key is valid on enum set
- `search()` Return key for searched value

### dynamic methods

[](#dynamic-methods)

```
class UserStatus extends BitMask
{
    private const Registered = 1;
    private const Active = 2;
}

// Static method:
$status = UserStatus::Registered();
$status = UserStatus::Active();

// instance methods
$status->isRegistered();
$status->isActive();
$status->setRegistered($bool);
$status->setActive($bool);
```

Static method helpers are implemented using [`__callStatic()`](http://www.php.net/manual/en/language.oop5.overloading.php#object.callstatic) and [`__call()`](https://www.php.net/manual/en/language.oop5.overloading.php#object.call).

If you care about IDE autocompletion, you can either implement the static methods yourself:

```
class UserStatus extends BitMask
{
    private const Registered = 1;

    /**
     * @return UserStatus
     */
    public static function Registered() {
        return new UserStatus(self::Registered);
    }
    /**
     * @return bool
     */
    public function isRegistered(){
        return $this->isFlagSet(self::Registered);
    }
    /**
     * @param bool $flag
     * @return self
     */
    public function setRegistered(bool $flag){
        return $this->setFlag(self::Registered, $flag);
    }
}
```

Or you can use phpdoc (this is supported in PhpStorm for example):

```
/**
 * @method static UserStatus Registered()
 * @method bool isRegistered()
 * @method UserStatus setRegistered(bool)
 */
class UserStatus extends Bitmask
{
    private const Registered = 1;
}
```

Related projects
----------------

[](#related-projects)

- \[myclabs/php-enum\] ()

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance53

Moderate activity, may be stable

Popularity29

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 76.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~335 days

Total

5

Last Release

976d ago

PHP version history (2 changes)v0.1.0PHP &gt;=7.1

0.2.0PHP &gt;=7.1 | &gt;=8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/660f28cae8e83d7fa7ebc774ed57430be88f4b66b9f7e4ff07591c74297a7d15?d=identicon)[CyberiaResurrection](/maintainers/CyberiaResurrection)

---

Top Contributors

[![c-harris](https://avatars.githubusercontent.com/u/16450074?v=4)](https://github.com/c-harris "c-harris (56 commits)")[![CyberiaResurrection](https://avatars.githubusercontent.com/u/9083866?v=4)](https://github.com/CyberiaResurrection "CyberiaResurrection (14 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (3 commits)")

---

Tags

enumbitmask

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/cruxinator-php-bitmask/health.svg)

```
[![Health](https://phpackages.com/badges/cruxinator-php-bitmask/health.svg)](https://phpackages.com/packages/cruxinator-php-bitmask)
```

###  Alternatives

[myclabs/php-enum

PHP Enum implementation

2.7k227.9M637](/packages/myclabs-php-enum)[dasprid/enum

PHP 7.1 enum implementation

382146.0M11](/packages/dasprid-enum)[spatie/enum

PHP Enums

84529.1M68](/packages/spatie-enum)[marc-mabe/php-enum

Simple and fast implementation of enumerations with native PHP

49644.8M97](/packages/marc-mabe-php-enum)[spatie/laravel-enum

Laravel Enum support

3655.4M31](/packages/spatie-laravel-enum)[timeweb/phpstan-enum

Enum class reflection extension for PHPStan

443.2M21](/packages/timeweb-phpstan-enum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
