php类常量调用$ this :: CONST

时间:2016-03-01 09:04:24

标签: php oop constants

为什么我可以从动态声明中调用类常量? 这段代码效果很好:

echo $this::CONST;

不是吗?

1 个答案:

答案 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
?>

从动态声明中调用常量与从类

调用它一样有效