composer require veejay/bitmaskBasic usage without restrictions.
<?php
use Veejay\Bitmask\Bitmask;
$bitmask = new Bitmask(0b11);
$bitmask->get(); // 3
$bitmask->add(0b110);
$bitmask->get(); // 7
$bitmask->has(0b10); // true
$bitmask->has(0b1000); // false
$bitmask->remove(0b100);
$bitmask->get(); // 3
$bitmask->clear();
$bitmask->get(); // 0Usage with available flags list.
<?php
use Veejay\Bitmask\BitmaskList;
$flags = [
1 => 'Has fangs',
// 2 => missed
4 => 'Has tail',
8 => 'Has paws',
];
$bitmask = new BitmaskList(0, $flags);
$bitmask->add(1);
$bitmask->get(); // 1
$bitmask->add(2); // ExceptionExtending class and creating constants.
<?php
use Veejay\Bitmask\BitmaskList;
class Beast extends BitmaskList
{
public const FANGS = 1;
public const TAIL = 4;
public const PAWS = 8;
protected array $labels = [
self::FANGS => 'Has fangs',
// 2 => missed
self::TAIL => 'Has tail',
self::PAWS => 'Has paws',
];
}
$bitmask = new Beast(Beast::FANGS | Beast::PAWS);
$bitmask->get(); // 9
$bitmask->set(Beast::TAIL | Beast::PAWS);
$bitmask->get(); // 12
$bitmask->has(Beast::PAWS); // true
$bitmask->has(Beast::FANGS); // false