在PHP中使用存储在const中的属性名

在PHP中使用存储在const中的属性名,php,constants,Php,Constants,我想访问名称存储在常量中的属性 class Foo { const PROPERTY_NAME = 'bar'; protected $bar; public function getBar() { return $this->self::PROPERTY_NAME; } } 你知道怎么做吗?使用 试试这个: class MyClass { const MYCONSTANT = 'constant value';

我想访问名称存储在常量中的属性

class Foo
{
     const PROPERTY_NAME = 'bar';

     protected $bar;

     public function getBar() {
         return $this->self::PROPERTY_NAME;
     }

}
你知道怎么做吗?

使用

试试这个:

class MyClass
{
    const MYCONSTANT = 'constant value';

    function showConstant() {
        echo  self::MYCONSTANT. "\n";
    }
}

$classname = "MyClass";
echo $classname::MYCONSTANT. "\n"; // As of PHP 5.3.0
//输出:定值

class MyClass
{
    const MYCONSTANT = 'constant value';

    function showConstant() {
        echo  self::MYCONSTANT. "\n";
    }
}

$classname = "MyClass";
echo $classname::MYCONSTANT. "\n"; // As of PHP 5.3.0