Can classes have bitshifted values defined as class constants / static variables ?
I want to implement a permission system similar to the one described here http://www.litfuel.net/tutorials/bitwise.htm (sorry best example I could find from a quick google)
For example ive tried this
which gives
class permissions {
const perm1 =1;
const perm2 =2;
const perm3 =4;
//--CUT--
const perm24 =8388608;const perm25 = perm1 | perm2;
}
syntax error, unexpected '|', expecting ',' or ';'
and the 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 a class environment eg
$perm1 =1<<0;
$perm2 =1<<1;
$perm3 =1<<2;
//--CUT--
$perm24 =1<<23;
$perm25 = $perm1 | $perm2;
echo $perm25;
Giving the expected 3 (2+1) (or 2^0 + 2^1)
What is the best way to define this in a class ?
Answer
Quoting from the docs:
The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call.
bitwise or logical operations qualify (like mathematical operations) as not permitted
No comments:
Post a Comment