如何从父类获取子类名

Web*_*net 6 php oop late-static-binding php-5.3

我试图在不需要子类的功能的情况下完成这个...这可能吗?我有一种感觉它不是,但我真的想确定...

<?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
?>
Run Code Online (Sandbox Code Playgroud)

Bol*_*ock 13

get_called_class()而不是__CLASS__.您也可以替换static使用self的功能将解决该类通过对你后期绑定:

class A {
    public static function who() {
        echo get_called_class();
    }
    public static function test() {
        self::who();
    }
}

class B extends A {}

B::test();
Run Code Online (Sandbox Code Playgroud)