Bitmask在PHP中进行设置?

前端之家收集整理的这篇文章主要介绍了Bitmask在PHP中进行设置?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
位和位掩码是我一直在努力理解一段时间的东西,但是我想学习如何使用它们进行 PHP中的设置和设置.

我终于找到了一个声称要这样做的课程,我可以告诉我,这似乎是有效的,但我不知道这是否是最好的方法.我将以下面的示例代码发布类文件,以便正常运行.

请,如果你有经验,告诉我是否可以改进,性能或任何其他.我真的想学习这个,我一直在阅读,但是对我来说这是一个困难的事情.

班上…

<?PHP
    class bitmask
    {
        /**
         * This array is used to represent the users permission in usable format.
         *
         * You can change remove or add valuesto suit your needs.
         * Just ensure that each element defaults to false. Once you have started storing
         * users permsisions a change to the order of this array will cause the
         * permissions to be incorectly interpreted.
         *
         * @type Associtive array
         */
        public $permissions = array(
                                    "read" => false,"write" => false,"delete" => false,"change_permissions" => false,"admin" => false
                                    );

        /**
         * This function will use an integer bitmask (as created by toBitmask())
         * to populate the class vaiable
         * $this->permissions with the users permissions as boolean values.
         * @param int $bitmask an integer representation of the users permisions.
         * This integer is created by toBitmask();
         *
         * @return an associatve array with the users permissions.
         */
        public function getPermissions($bitMask = 0)
        {
            $i = 0;
            foreach ($this->permissions as $key => $value)
            {
                $this->permissions[$key] = (($bitMask & pow(2,$i)) != 0) ? true : false;

                // Uncomment the next line if you would like to see what is happening.
                //echo $key . " i= ".strval($i)." power=" . strval(pow(2,$i)). "bitwise & = " . strval($bitMask & pow(2,$i))."<br>";
                $i++;
            }
            return $this->permissions;
        }

        /**
         * This function will create and return and integer bitmask based on the permission values set in
         * the class variable $permissions. To use you would want to set the fields in $permissions to true for the permissions you want to grant.
         * Then call toBitmask() and store the integer value.  Later you can pass that integer into getPermissions() to convert it back to an assoicative
         * array.
         *
         * @return int an integer bitmask represeting the users permission set.
         */
        function toBitmask()
        {
            $bitmask = 0;
            $i = 0;
            foreach ($this->permissions as $key => $value)
            {

                if ($value)
                {
                    $bitmask += pow(2,$i);
                }
                $i++;
            }
            return $bitmask;
        }
    }
?>

如何将权限设置/保存为位掩码值?

<?PHP
    /**
     * Example usage
     * initiate new bitmask object
     */
    $perms = new bitmask();

    /**
     * How to set permissions for a user
     */
    $perms->permissions["read"] = true;
    $perms->permissions["write"] = true;
    $perms->permissions["delete"] = true;
    $perms->permissions["change_permissions"] = true;
    $perms->permissions["admin"] = false;

    // Converts to bitmask value to store in database or wherever
    $bitmask = $perms->toBitmask();  //in this example it is 15
    $sql = "insert into user_permissions (userid,permission) values(1,$bitmask)";
    echo $sql; //you would then execute code to insert your sql.
?>

使用位掩码值并根据位值返回每个数组项的true / false的示例….

<?PHP
    /**
     * Example usage to get the bitmask value from database or session/cache.... then put it to use.
     * $permarr returns an array with true/false for each array value based on the bit value
     */
    $permarr = $perms->getPermissions($bitmask);

    if ($permarr["read"])
    {
        echo 'user can read: <font color="green">TRUE</font>';
    }
    else {
        echo 'user can read: <font color="red">FALSE</font>';
    }

    //user can WRITE permission
    if ($permarr["write"])
    {
        echo '<br>user can write: <font color="green">TRUE</font>';
    }
    else {
        echo '<br>user can write: <font color="red">FALSE</font>';
    }
?>
位字段是处理标志或一组布尔值的非常方便和有效的工具.

要了解他们,您首先需要知道二进制数字的工作原理.之后,您应该查看bitwise operators上的手册条目,并确保您知道按位AND,OR和左/右移位是如何工作的.

一个位字段只不过是一个整数值.我们假设我们的位字段的大小是固定的,只有一个字节.电脑使用二进制数字,所以如果我们的号码是29,你会在内存中找到0001 1101.

使用按位AND(&)和按位OR(|),您可以读出并单独设置数字的每一位.它们都以两个整数作为输入,并分别对每个位执行AND / OR.

要读出你的号码的第一位,你可以这样做:

0001 1101 (=29,our number)
& 0000 0001 (=1,bit mask)
= 0000 0001 (=1,result)

正如你所看到的,你需要一个特殊的数字,只有我们感兴趣的位被设置,这就是所谓的“位掩码”.在我们的例子中,它是1.要读出第二个位,我们必须将位图中的一个“push”到左边一位.我们可以用左移位运算符($number <1)或乘以2来做到这一点.

0001 1101
& 0000 0010
= 0000 0000 (=0,result)

你可以为我们号码中的每一位做到这一点.我们的数字和位掩码的二进制AND导致零,这意味着该位不是“置位”,或者是非零整数,这意味着该位被置位.

如果要设置其中一个位,可以使用按位OR:

0001 1101
| 0010 0000 (=32,bit mask)
= 0011 1101 (=29+32)

但是,当你想要“清除”一点时,你必须采取不同的方式.

更一般的做法是:

// To get bit n
$bit_n = ($number & (1 << $n)) != 0
// Alternative
$bit_n = ($number & (1 << $n)) >> $n

// Set bit n of number to new_bit
$number = ($number & ~(1 << $n)) | ($new_bit << $n)

起初它可能看起来有点神秘,但其实很简单.

到目前为止,你可能会发现,比特字段是一个很低级的技术.这就是为什么我建议不要在PHP数据库中使用它们.如果你想要一堆标志,这可能是确定的,但是对于其他任何你真正不需要的标志.

你发布的课程对我来说有点特别.例如,像…的东西?真的:假的是糟糕的做法.如果你想使用位字段,你最好定义一些常量,并使用上面描述的方法.提出一个简单的课不难.

define('PERM_READ',0);
define('PERM_WRITE',1);

class BitField {
    private $value;

    public function __construct($value=0) {
        $this->value = $value;
    }

    public function getValue() {
        return $this->value;
    }

    public function get($n) {
        return ($this->value & (1 << $n)) != 0;
    }

    public function set($n,$new=true) {
        $this->value = ($this->value & ~(1 << $n)) | ($new << $n);
    }
    public function clear($n) {
        $this->set($n,false);
    }
}


$bf = new BitField($user->permissions);

if ($bf->get(PERM_READ)) {
    // can read
}

$bf->set(PERM_WRITE,true);
$user->permissions = $bf->getValue();
$user->save();

我没有尝试任何这个答案的代码,但它应该让你开始,即使它不是开箱即用.

请注意,您每位字段限制为32个值.

猜你在找的PHP相关文章