【发布时间】:2011-01-26 16:26:23
【问题描述】:
我从 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(父类)中的一个函数需要使用该常量。有什么想法吗?
【问题讨论】: