从php中的子类获取数组中的常量

时间:2015-11-05 16:45:40

标签: php inheritance reflection constants

我有以下问题。我有一个只包含常量的子类。我在父类中有变量常量。我需要在父类中将常量作为变量。 我试试

$onClass = new ReflectionClass(__CLASS__);
$this->constants = $onClass->getConstants();

但是只有在子构造函数中调用它时才会出现这种情况。我需要在父母的帮助中打电话给我。有没有可能如何做到这一点?

非常感谢

1 个答案:

答案 0 :(得分:0)

您的实现仅违反继承模型。您可能需要考虑重构。

这听起来像你当前的设置:

docker run -d -P -e "MONGO_DB=host:port/database" yourcorp/node-app supervisor app.js

您将无法在A类声明中调用getConstants。 A类没有定义这样的函数,也不知道(或关心)它是否是B类的实例。

几乎听起来你的层次结构已经逆转了:

class A {
    function __construct() {
        $this->constants = $this->getConstants()
    }
}

class B extends A {
  public function getConstants() {
      return [1, 2, 3];
  }
}