如何从家长班获得孩子班的名字 [英] How to get child class name from parent class
本文介绍了如何从家长班获得孩子班的名字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在不需要子类上函数的情况下完成此任务……这可能吗?我感觉不是,但是我真的想确定...
I'm trying to accomplish this without requiring a function on the child class... is this possible? I have a feeling it's not, but I really want to be sure...
<?php
class A {
public static function who() {
echo __CLASS__;
}
public static function test() {
static::who(); // Here comes Late Static Bindings
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::test(); //returns B
?>
推荐答案
使用 __CLASS__
.您还可以将static
替换为self
,因为该函数将通过后期绑定为您解决该类:
Use get_called_class()
instead of __CLASS__
. You'll also be able to replace static
with self
as the function will resolve the class through late binding for you:
class A {
public static function who() {
echo get_called_class();
}
public static function test() {
self::who();
}
}
class B extends A {}
B::test();
这篇关于如何从家长班获得孩子班的名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文