我是php的新手,在this页面上尝试使用php登录系统但是有点混淆了一段代码。
以下提供的代码段。
<?php
class UserPermissions {
const READ_POSTS = 1;
const POST_NEW_THREADS = 2;
const POST_NEW_REPLIES = 4;
const EDIT_OWN_POSTS = 8;
const EDIT_OTHERS_POSTS = 16;
const DELETE_OWN_POSTS = 32;
const DELETE_OTHERS_POSTS = 64;
const MOVE_THREADS = 128;
const SPLIT_THREADS = 256;
const MERGE_THREADS = 512;
const BAN_USERS = 1024;
const WARN_USERS = 2048;
const ACCESS_ADMIN_PANEL = 4096;
// And so on and so on
protected $perms;
function __construct($permissions) {
$this->perms = $permissions;
}
function hasPermission($perm) {
return ($this->perms & $perm) === $perm;
}
}
?>
主要关注的是hasPermision函数以及如何计算返回值。
答案 0 :(得分:0)
PHP.net将是您最好的朋友。以下是对比较器问题的解释:http://php.net/manual/en/language.operators.comparison.php
基本上,$ perm必须相同但也属于同一类型。
以下是按位文档的链接:http://php.net/manual/en/language.operators.bitwise.php