oop - php class constants with bitvalues -


can classes have bitshifted values defined class constants / static variables ?

i want implement permission system similar 1 described here http://www.litfuel.net/tutorials/bitwise.htm (sorry best example find quick google)

for example ive tried this

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

const perm25 = perm1 | perm2;

}

gives

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

and preferred way

class permissions { static $perm1 =1<<0; static $perm2 =1<<1; static $perm3 =1<<2; //--cut-- static $perm24 =1<<23;  static $perm25 = $perm1 | $perm2;  }   

which gives

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

the latter way works outside of class environment eg

$perm1 =1<<0; $perm2 =1<<1; $perm3 =1<<2; //--cut-- $perm24 =1<<23;  $perm25 = $perm1 | $perm2; echo $perm25; 

giving expected 3 (2+1) (or 2^0 + 2^1)

what best way define in class ?

quoting docs:

the value must constant expression, not (for example) variable, property, result of mathematical operation, or function call.

bitwise or logical operations qualify (like mathematical operations) not permitted


Comments