2013-05-08 46 views
0

類可以有位移值定義爲類常量/靜態變量嗎?php類常量與位值

我想要實現類似於這裏所描述的一個權限系統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)

在課堂上定義這個最好的方法是什麼?

+0

使用的__construct方法 – machineaddict 2013-05-08 22:09:27

+0

'$ PERM1 = 1 << 0;'不限定恆定;它定義了一個變量 – 2013-05-08 22:11:42

回答

0

docs引用:

該值必須是一個常量表達式,而不是(例如)一個變量,屬性,數學運算的結果,或一個函數調用。

按位或邏輯操作資格(如數學運算)作爲不允許

+0

指出我在正確的方向我現在使用十六進制符號來保持它的可讀性,但實現類似的事情 – exussum 2013-05-08 22:49:57

+0

更乾淨的方式做它 – 2013-05-08 22:56:32