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))."
";

$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;

}

}

?>

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

/**

* 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的示例。

/**

* 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: TRUE';

}

else {

echo 'user can read: FALSE';

}

//user can WRITE permission

if ($permarr["write"])

{

echo '
user can write: TRUE';

}

else {

echo '
user can write: FALSE';

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值