PHP中是否存在保存当前子类名称的常量?这样我就可以编写如下的函数:
namespace test\that;
class MyClass extends ClassA {
}
abstract class ClassA {
public static function getClassName() {
return __THIS_CLASS__; // like get_class($this); in a none static method
}
}最后我会做这样的事情:
namespace test\other;
use test\that\MyClass;
var_dump(MyClass::getClassName()); // --> test\that\MyClass这可能吗?
发布于 2014-04-22 07:26:35
您可能正在查看get_called_class();它返回静态方法被调用的类的名称。
public static function getClassName() {
return get_called_class();
}
MyClass::getClassName(); // "MyClass"发布于 2014-04-22 07:25:29
__CLASS__怎么样?
查看常量:http://www.php.net/manual/en/language.constants.predefined.php
发布于 2014-04-22 07:27:02
您可以使用PHP的get_called_class()方法。若要同时返回父类和子类名,请执行以下操作。
https://stackoverflow.com/questions/23213107
复制相似问题