【问题标题】:PHP - per class configuration manager and inheritancePHP - 每个类配置管理器和继承
【发布时间】:2012-10-05 18:58:42
【问题描述】:

我有一个 Injector 类,它使用 get_class($this) 来使用 Injector 获取类,从 db 或文件中加载类中的一些数据......它适用于像 class User extends Injector 这样的简单继承。

问题是:如果一个类SpecialUser 扩展Userget_class 只返回SpecialUserUser 将不会填充所需的数据。有没有更好的解决方案,然后使用 get_parent_class() 循环遍历类?

谢谢

【问题讨论】:

  • 嗨,欢迎来到 stackoverflow!请阅读this

标签: php dependency-injection multiple-inheritance


【解决方案1】:

您可以使用以下内容:

$names = array();

if ($this instanceof Injector) {
    $class = get_class($this);

    do {
        $names[] = $class;
        $class = get_parent_class($class);
    } while ($class !== 'Injector');
}

Here's an example of its usage.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 2011-01-29
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多