本文介绍了使用常量作为类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用常量作为此类静态属性访问权限的类名称,即

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;

这将返回错误,表明类CLASSNAME不存在.有解决办法吗?

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

推荐答案

可以通过反射来实现:

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...

这篇关于使用常量作为类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:46
查看更多