PHPackages                             henrywhitaker3/bitmasked-properties - 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. henrywhitaker3/bitmasked-properties

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

henrywhitaker3/bitmasked-properties
===================================

A package to use bitmasked properties on classes

v1.0.0(4y ago)07MITPHPPHP ^8.1

Since Nov 23Pushed 4y ago1 watchersCompare

[ Source](https://github.com/henrywhitaker3/bitmasked-properties)[ Packagist](https://packagist.org/packages/henrywhitaker3/bitmasked-properties)[ RSS](/packages/henrywhitaker3-bitmasked-properties/feed)WikiDiscussions main Synced 2d ago

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

Bitmasked Properties
====================

[](#bitmasked-properties)

This library provides a trait to use bitmasked properties with PHP 8.1 enums. I was reading [this](https://aaronfrancis.com/2021/bitmasking-in-laravel-and-mysql) 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
------------

[](#installation)

```
composer require henrywhitaker3/bitmasked-properties
```

Usage
-----

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

```
class Person
{
    public function __construct(
        public bool $sms,
        public bool $email
    ) {}

    public function isOptedIn(string $type): bool
    {
        // Should really check the value of type is valid first...
        return $this->{$type};
    }

    public function optin(string $type): void
    {
        $this->updateOptin($type, true);
    }

    public function optout(string $type): void
    {
        $this->updateOptin($type, false);
    }

    private function updateOptin(string $type, bool $value): void
    {
        switch($type) {
            case 'sms':
                $this->sms = $value;
                break;
            case 'email':
                $this->email = $value;
                break;
        }
    }
}
```

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:

```
enum Optin: int implements BitmaskEnum
{
    case SMS = 1 setFlag('optin', $type, true);
    }

    public function optout(Optin $type): void
    {
        $this->setFlag('optin', $type, false);
    }

    public function getOptins(): WeakMap
    {
        return $this->flagToWeakmap('optin', Optin::class);
    }
}
```

Now it's really simple to use:

```
$person = new Person; // $optin === 0

$person->isOptedIn(Optin::SMS); // returns false
$person->optin(Optin::SMS); // $optin === 1
$person->isOptedIn(Optin::SMS); // returns true

$person->optin(Optin::EMAIL); // $optin === 3
$person->isOptedIn(Optin::EMAIL); // returns true

$person->optout(Optin::SMS); // $optin === 2
$person->isOptedIn(Optin::SMS); // returns false

$person->getOptins()[Optin::EMAIL]; // return true
```

You can add a new value to the `Optin` enum with no changes to the database or code.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

1631d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0396f60bf3898a46347980c41828d65f3de5b2e621ffe4e293c146d48be1176d?d=identicon)[henrywhitaker3](/maintainers/henrywhitaker3)

---

Top Contributors

[![henrywhitaker3](https://avatars.githubusercontent.com/u/36062479?v=4)](https://github.com/henrywhitaker3 "henrywhitaker3 (5 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/henrywhitaker3-bitmasked-properties/health.svg)

```
[![Health](https://phpackages.com/badges/henrywhitaker3-bitmasked-properties/health.svg)](https://phpackages.com/packages/henrywhitaker3-bitmasked-properties)
```

PHPackages © 2026

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