php - 从抽象类中的静态函数获取类名

标签 php class static abstract

我有一个抽象类,它有许多静态函数(它通过使用 new static($args) 返回一个自身的新实例,效果很好),但我无法解决如何获取类名。我尽量避免放置

protected static $cn = __CLASS__;

但如果不可避免,那还不是世界末日

abstract class ExtendableObject {
    static function getObject() {
        return new static($data);
    }

    static function getSearcher() {
        return new ExtendableObjectFinder(/* CLASS NAME CLASS */);
    }
}

class ExtendableObjectFinder {
    private $cn;

    function __construct($className) {
       $this->cn = $className;
    }

    function where($where) { ... }

    function fetch() { ... }
}

最佳答案

要获取类的名称,您可以使用 get_class并传递 $this

或者,有 get_called_class您可以在静态方法中使用它。

关于php - 从抽象类中的静态函数获取类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13121864/

相关文章:

php - 创建表后,它显示此数组( [0] => 00000 [1] => [2] => )

php - 如何保存扩展名为 .php 或 .html 的 PHP 文件?

php - 使用PHP从public_html外部读取图像?

c++ - 无法在 C++ 中调用 const 引用参数的方法

php - 在 PHP 中获取一个类的所有实例

c++ - 表达式左侧的函数调用

c++ - 使用许多静态成员有问题吗?

php - 计算两个邮政编码之间的距离

c++ - C++03 和 C++11 类的区别

java - 对象中的无限递归/静态创建/为什么此代码会抛出 NullPointerException?