Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

class const dynamic called

I want to dynamic get the class const.

class A
{
  const test="A";

  public static function getConst()
  {
    string className=(string)get_called_class();
    echo {className}::test;
  }
}

class B extends A
{
  const test="B";
}

the line : echo {className}::test; has error.

How should I realize.



1.1k
edited Jun '16

Try:

string className;
let className = (string) get_called_class();


4.5k

has error also.



4.5k
edited Jun '16

direct

echo {"A"}::test;

error also.



4.5k
edited Jun '16

and I use

self::test

error also.



136
Accepted
answer

You can use http://php.net/manual/en/function.constant.php procedure

echo constant(get_class(this) . "::test");
// or
echo constant(get_called_class() . "::test");

not optimized way, but work.



4.5k

Thank you. It can work now.