带有bitvalues的php类常量

时间:2013-05-08 21:43:30

标签: php oop bit-manipulation

类可以将位移值定义为类常量/静态变量吗?

我想实现类似于此处所述的权限系统http://www.litfuel.net/tutorials/bitwise.htm(对不起,我可以从快速谷歌中找到最好的例子)

例如香港专业教育学院试过这个

提供

class permissions {
const perm1 =1;
const perm2 =2;
const perm3 =4;
//--CUT--
const perm24 =8388608;

const perm25 = perm1 | perm2;

}

syntax error, unexpected '|', expecting ',' or ';'

和首选方式

class permissions {
static $perm1 =1<<0;
static $perm2 =1<<1;
static $perm3 =1<<2;
//--CUT--
static $perm24 =1<<23;

static $perm25 = $perm1 | $perm2;

}  

给出了

syntax error, unexpected T_SL, expecting ',' or ';'

后一种方式在类环境之外工作,例如

$perm1 =1<<0;
$perm2 =1<<1;
$perm3 =1<<2;
//--CUT--
$perm24 =1<<23;

$perm25 = $perm1 | $perm2;
echo $perm25;

给出预期的3(2 + 1)(或2 ^ 0 + 2 ^ 1)

在课堂上定义此内容的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

引用docs

  

值必须是常量表达式,而不是(例如)变量,属性,数学运算的结果或函数调用。

按位或逻辑运算限定(如数学运算)不允许