【发布时间】:2013-05-03 06:37:29
【问题描述】:
类可以将位移值定义为类常量/静态变量吗?
我想实现一个类似于此处描述的权限系统http://www.litfuel.net/tutorials/bitwise.htm(抱歉,我可以从快速谷歌中找到最好的例子)
例如我试过这个
类权限{ 常量 perm1 =1; 常量 perm2 =2; 常数 perm3 =4; // - 切 - 常量 perm24 =8388608;常量 perm25 = perm1 |烫发2;
}
给出 语法错误,意外的 '|',需要 ',' 或 ';'以及首选方式
class permissions {
static $perm1 =1<<0;
static $perm2 =1<<1;
static $perm3 =1<<2;
//--CUT--
static $perm24 =1<<23;
static $perm25 = $perm1 | $perm2;
}
这给了
语法错误,意外的 T_SL,需要 ',' 或 ';'后一种方式在类环境之外工作,例如
$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)
在类中定义它的最佳方式是什么?
【问题讨论】:
-
为此使用 __construct 方法
-
$perm1 =1<<0;没有定义常量;它定义了一个变量
标签: php oop bit-manipulation