为什么我可以从动态声明中调用类常量? 这段代码效果很好:
echo $this::CONST;
不是吗?
答案 0 :(得分:0)
来自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
?>
从动态声明中调用常量与从类
调用它一样有效