get_call_class()总是返回你实际调用的类。你打电话给A2::所以它是 A2。
在我的网站上有一个LSB 单例抽象类的教程。我没有在这里链接,因为总是有一个僵尸义务警员来了,甚至不看就删除了链接。但在我的描述中。
LSB 的问题是 A 中的方法可以调用 B 中的方法,而 B 中的方法可以调用 A 中的方法。看这个例子:
header('Content-Type: text/plain'); // Pretty text output
// LSB Parent
class A {
// NOLSB: Will call Current Class method
static public function TriggerSelf() {
self::OverrideMe();
}
// LSB: Will call Inheriting Class method (or Current if none Inherits)
static public function TriggerStatic() {
static::OverrideMe();
}
// Method to Override
static public function OverrideMe() {
echo 'A here', PHP_EOL;
}
}
// LSB Child
class B extends A {
// Override by LSB
static public function OverrideMe() {
echo 'B here', PHP_EOL;
}
}
A::TriggerSelf(); // <- NO LSB
A::TriggerStatic(); // <- LSB (but not inheritance)
B::TriggerSelf(); // <- NO LSB
B::TriggerStatic(); // <- LSB (with inheritance, so it works)
了解B::TriggerStatic()如何允许 A 调用 B 方法,而B::TriggerSelf()调用 A 方法。那是LSB。父类静态方法可以调用子类静态方法。这几乎是静态摘要:)
研究这个例子,它会很有意义。