为什么我可以从动态声明调用类常量?这段代码效果很好:
echo $this::CONST;
难道没有错吗?
来自 http://php.net/manual/en/language.oop5.constants.php
<?php
class MyClass
{
const CONSTANT = 'constant value';
function showConstant() {
echo self::CONSTANT . "'n";
}
}
echo MyClass::CONSTANT . "'n";
$classname = "MyClass";
echo $classname::CONSTANT . "'n"; // As of PHP 5.3.0
$class = new MyClass();
$class->showConstant();
echo $class::CONSTANT."'n"; // As of PHP 5.3.0
?>
从动态声明调用常量与从类调用常量一样有效