一个php操作状态位状态的类

平时经常有通过二进制位来存储多个开关位,实现一个字段存多个开关的功能。
每次操作位,难免容易出错,遂写了一个类简化操作,闲话少说上代码和使用方法。
BitState类:

<?php
class BitState {
    private $maps = [];
    private $state = 0;
    public function __construct($state = 0,$maps){
        $this->state = $state;
        $this->maps = $maps;
    }
    public function setOn($k) {
        $bit = $this->stateBit($k);
        $this->state = $this->state|$bit;
        return $this->state;
    }
    public function setOff($k) {
        $bit = $this->stateBit($k);
        $this->state =  $this->state&(~$bit);
        return $this->state;
    }
    public function check($k) {
        $bit = $this->stateBit($k);
        if (($this->state & $bit) == $bit) {
            return 1;
        }
        return 0;
    }
    public function __toString(){
        return strval($this->state);
    }

    public function getState() {
        return $this->state;
    }
    public function getBinState() {
        return decbin($this->state)
    }
    private function stateBit($k) {
        if (isset($this->maps[$k])) {
            return $this->maps[$k];
        }
        return null;
    }
}

用法:
//初始化状态
$maps = [
'apple' => 1<<0,
'banana' => 1<<1,
'peach' => 1<<2,
];
//demo1
//创建一个状态值(初始为0)
$state = new BitState(0,$maps);
//设置这个状态值apple 位置 1
$state->setOn('apple');
//设置这个状态值peach 位置 1
$state->setOn('peach');
//输出这个值(10进制)
echo $state->getState(); //结果为 5
//或
echo $state; //结果为 5
//输出这个值二进制位形式(101)
echo $state->getBinState()

//demo2
//创建一个基于已有状态的状态位 (初始化4)
$state = new BitState(4,$maps); // 即初始化为 010
$state->setOn("apple");
$state->setOff("banana");
echo $state; // 输出1 即 001

已有 10 条评论
  1. Austot

    Viagra Dapoxetine Avis Propecia Eyebrow Growth viagra Viagra Billig Per Nachnahme Tadalafil E20 Canada Online Precio Levitra 10 Mg

    Austot 回复
  2. FranKap

    Clomid Usa Shippments Online viagra Propecia Liver Damage Metabolized Buy Levothyroxine 88 Mcg buy accutane paypal

    FranKap 回复
  3. Ellcurfak

    Amoxicillin Virus Maculopapular Cialis Espana Elontril Online cheap cialis Kamagra Efectos Ajanta Pharma Cephalexin And Angioedema In Dogs Acquistare Cialis Online

    Ellcurfak 回复
  4. FranKap

    Generisches Priligy Kaufen Viagra Boisson Propecia Worked For Me Receding Hairline buy cialis Buy Deltasone Prednisone

    FranKap 回复
  5. RandPsymn

    Amoxicillin For Bronchial Infections cialis Levitra Avantage

    RandPsymn 回复
  6. Ellcurfak

    Cialis 5 Mg Prezzo Farmacia Avana Pills accutane discount card cialis Osu Cialis Commander Can You Buy Valtrex At Walgreens Erectile Dysfunction Medication Prices

    Ellcurfak 回复
  7. Austot

    Viagra Tabletten Wirkung buy viagra Viagra 100 Mg Pulmonary Hypertension Vente Cialis Generique Ligne Propecia Cigartec

    Austot 回复
  8. LesSity

    Keflex Dental Premed Commander Acheter Levitra En Ligne Didanosine viagra Keflex And Calcium Priligy Does It Work Nolvadex Dose

    LesSity 回复
  9. FranKap

    Vergleich Levitra generic cialis Cheap Tadalista

    FranKap 回复
  10. Ellcurfak

    Precio De Propecia Espana cialis price Compra Cialis Generico Cialis Oder Levitra Metformin Fast Delivery

    Ellcurfak 回复
发表新评论