Bitmask is a simple PHP implementation of bitwise operations for creating masks. Can be used for some flags' implementation. Supports only 64 bits (from 0 to 63) on x64 platforms.
Install the latest version with composer:
composer require aliance/bitmaskIf you checkout this library for testing purposes, install its dependencies:
docker run --rm -it --volume $PWD:/app -u $(id -u):$(id -g) composer:2 iPass a bit position (0 through 63) to setBit(), unsetBit() and issetBit().
Pass an integer mask to addMask() and deleteMask(). For example, starting from an empty mask:
use Aliance\Bitmask\Bitmask;
(new Bitmask())->setBit(3)->getMask(); // 8 (bit 3)
(new Bitmask())->addMask(3)->getMask(); // 3 (bits 0 and 1)The permission example uses an integer-backed enum to name bit positions.
It loads a user with Create permission, grants Read, and saves the updated value with getMask().
Loading that value restores both permissions.
PHP uses signed integers. Setting bit 63 makes the mask negative.
Use a signed 64-bit column such as BIGINT to store the full range and pass the signed value back to the constructor when reading it.
docker run --rm -v "$PWD":/usr/src/bitmask -w /usr/src/bitmask php:8.3-cli php example/example.phpStored mask: 3
Create: yes
Read: yes
Update: no
Delete: no
For completely tests running just call composer exec phpunit or use
docker run -it --rm -v "$PWD":/usr/src/bitmask -w /usr/src/bitmask php:8.3-cli php ./vendor/bin/phpunit This software is distributed under MIT license.