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__ PHP。需要对此进行澄清。
问题是:为什么我需要识别类名来访问常量; 这是一个范围界定的问题吗?
是的,它的[类常量](http://php.net/manual/en/language.oop5.constants.php)。 – drew010
是的,但它为什么表现如此? – Valkyrurr
因为你已经这样宣布。 –