[英]Constant holding $this class name
Is there a constant in PHP holding the current sub classes name? PHP中是否有一个常量保存当前的子类名称? So that I can write a function like: 这样我就可以编写一个函数:
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
}
}
In the end I would do something like this: 最后我会做这样的事情:
namespace test\other;
use test\that\MyClass;
var_dump(MyClass::getClassName()); // --> test\that\MyClass
Is that even possible? 这甚至可能吗?
You're probably looking at get_called_class()
; 你可能正在看get_called_class()
; it returns the name of the class the static method is called in. 它返回调用静态方法的类的名称。
public static function getClassName() {
return get_called_class();
}
MyClass::getClassName(); // "MyClass"
How about __CLASS__
? __CLASS__
怎么__CLASS__
?
Have a look at the constants: http://www.php.net/manual/en/language.constants.predefined.php 看一下常量: http : //www.php.net/manual/en/language.constants.predefined.php
You can use PHP's get_called_class() method. 您可以使用PHP的get_called_class()方法。 To return both parent class and child class names. 返回父类和子类名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.