Bitmask
, (*1)
, (*2)
, (*3)
In PHP a number is (mostly) 4 Bytes long. This means that one number actually uses 32 bits of the internal storage., (*4)
In this case 32 boolean values can be stored as single integer. The problem ist a 'magic numbers'., (*5)
Installation
Install the latest version with, (*6)
composer require phoenixrvd/bitmask
Example
Without this API before.php, (*7)
<?php
class StateMap {
const OPTION_1 = 1;
const OPTION_2 = 2;
const OPTION_4 = 4;
// What is next ????
}
// Check for Active Feature
$activeFeatures = 6;
if(($activeFeatures & StateMap::OPTION_1) === StateMap::OPTION_1){
// Do this
}
if(($activeFeatures & StateMap::OPTION_2) !== StateMap::OPTION_2) {
// Do this
}
// Activation and deactivation from options ist not 'Human Readable'.
With this API after.php, (*8)
<?php
class StateMap {
const OPTION_1 = 0;
const OPTION_2 = 1;
const OPTION_4 = 2;
const OPTION_5 = 3;
const OPTION_6 = 4;
}
// Check for Active Feature
$activeFeatures = (new \PhoenixRVD\Bitmask\BitmaskFactory())->fromInt(6);
if($activeFeatures->isOn(StateMap::OPTION_1)){
// Do this
}
if($activeFeatures->isOff(StateMap::OPTION_2)) {
// Do that
}
// Activate options
$activeFeatures->on(StateMap::OPTION_5, StateMap::OPTION_6);
// Deactivate options
$activeFeatures->off(StateMap::OPTION_4, StateMap::OPTION_1);
Testing
composer bitmask:test
Copyright and license
Code released under the MIT License., (*9)