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

I want to access to a property whose name is stored in a const.

class Foo
{
     const PROPERTY_NAME = 'bar';

     protected $bar;

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

}

Any idea on how to do this ?

Use Variable variables

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

Fiddle

Try this:

class MyClass
{
    const MYCONSTANT = 'constant value';

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

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

// Output: constant value