【发布时间】:2016-04-15 00:21:58
【问题描述】:
class foo {
const bar1 = 10.0;
const bar2 = 2.22;
const bar3 = 5.12;
function getTotal($some_string, $some_number) {
$total = 0.0;
// Why would this flag a warning?
// $what_is_const = constant("bar" . $some_string); // DOES NOT WORK!
$what_is_const = constant(__CLASS__ . "::bar" . $some_string); // WORKS!
$total += $what_is_const * $some_number;
}
}
警告:常量():找不到常量//具有完全相同的名称!
问题是:为什么要识别类名才能访问常量; 是范围问题还是其他问题?
【问题讨论】:
-
是的,它是class constant。
-
是的,但为什么会这样呢?
-
因为你已经这样声明了。
-
这是否意味着如果它是一个类 const,为了访问 bar1 我必须明确声明
constant("foo::bar1");与constant("bar1"); // that is if bar1!=const相比?