在相同的抽象 class 定义中获取先前定义的常量值

Get a previously defined constant's value in the same abstract class definition

我正在尝试创建一个可以保存一些字符串的摘要 class。我希望每个常量都具有前一个常量的值和一些其他加法。例如,像这样:

abstract class Test
{
    const a = "a",
            b = "ab",
            c = "abc";
}

我不想将所有内容重复两次,因此当我想更改 a 时,我需要在每个值中进行更改。我希望它在下一个常量中使用前一个值,这样当我改变某些东西时,它都会被一个值改变。这样的事情可能吗?我试了this::a但是没有用,我也想不出别的办法。

Constant expression support was added in PHP 5.6:

It is possible to provide a scalar expression involving numeric and string literals and/or constants in context of a class constant.

这样,你可以做到:

const a = "a",
      b = self::a . "b",
      c = self::a . "bc";

您使用 self

在 类 中访问 const