Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use constant as class name

I need to use constant as class name for acces to this class static property, that is

class a {

    public static $name = "Jon";

}

define("CLASSNAME", "a");

echo CLASSNAME::$name;

this returns error, that class CLASSNAME not exists. There is some solution ?

like image 985
Oto Shavadze Avatar asked Feb 02 '13 13:02

Oto Shavadze


1 Answers

It's possible with reflection:

class a {

    public static $name = "Jon";

}

define("CLASSNAME", "a");

$obj = new ReflectionClass(CLASSNAME);
echo $obj->getStaticPropertyValue("name");

If it is a good design choice is another question...

like image 127
bitWorking Avatar answered Oct 30 '22 00:10

bitWorking