【发布时间】:2015-07-28 08:59:47
【问题描述】:
一个类常量似乎总是被解释为一个字符串,尽管它被定义为一个整数。为什么 PHP 会做这种类型的杂耍,我该如何防止呢?
见以下代码:
class BitSet {
const NONE = 0;
const FOO = 1;
const BAR = 2;
const ALL = 3;
public function __construct( $flags = self::NONE ) {
if( $flags & self::ALL !== $flags )
throw new \OutOfRangeException( '$flags = '.$flags.' is out of range' );
$this->value = $flags;
}
protected $value = self::NONE;
}
$bs = new BitSet( BitSet::FOO );
最后一行(构造函数的调用)抛出OutOfRangeException:
PHP Fatal error: Uncaught exception 'OutOfRangeException' with message '$flags = 1 is out of range' in test-case.php:12
Stack trace:
#0 /srv/www/matthiasn/class-constant-debug.php(19): BitSet->__construct('1')
#1 {main}
thrown in /srv/www/matthiasn/class-constant-debug.php on line 12
从回溯条目#0 可以清楚地看到,常量BitSet::FOO 作为字符而不是整数传递。因此,位掩码操作$flags & self::ALL !== $flags 不是在整数上执行,而是在按位 ASCII 表示上执行,因此失败。
什么鬼?!有没有比在任何地方都明确地使用(int)-cast 更好的方法来解决这个问题?
【问题讨论】:
-
你不认为
if( $flags &<-------需要成为&& -
@Uchiha 不,这是按位与运算。这意味着“如果两个操作数中的位都为 1,则生成一个位设置为 1 的值,否则为 0”。
-
我不确定为什么堆栈跟踪看起来像这样,但 var_dump 将其显示为 int:3v4l.org/mEYRZ
标签: php type-conversion class-constants