我从php.net看到了这个例子:

<?php
class MyClass {

     const MY_CONST = "yonder";

     public function __construct() {

          $c = get_class( $this );
          echo $c::MY_CONST;
     }
}

class ChildClass extends MyClass {

     const MY_CONST = "bar";
}

$x = new ChildClass(); // prints 'bar'
$y = new MyClass(); // prints 'yonder'
?>

但是$ c::MY_CONST仅在版本5.3.0或更高版本中可以识别。我正在写的课可能很多。

基本上,我已经在ChildClass中定义了一个常量,而MyClass(父亲类)中的一个函数需要使用该常量。任何的想法?

最佳答案

如何使用static::MY_CONST呢?

10-07 19:34
查看更多