类可以将位移值定义为类常量/静态变量吗?
我想实现类似于此处所述的权限系统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)
在课堂上定义此内容的最佳方法是什么?