类常量

类可能包含类常量, 这些常量在编译扩展后保持不变和不可更改。 类常量导出到 php 扩展, 允许从 php 中使用它们。

  1. namespace Test;
  2. class MyClass
  3. {
  4. const MYCONSTANT1 = false;
  5. const MYCONSTANT2 = 1.0;
  6. }

类常量可以使用类名和静态运算符 ::访问:

  1. namespace Test;
  2. class MyClass
  3. {
  4. const MYCONSTANT1 = false;
  5. const MYCONSTANT2 = 1.0;
  6. public function someMethod()
  7. {
  8. return MyClass::MYCONSTANT1;
  9. }
  10. }