PHP const不工作​​?

Mig*_*ork 2 php const class

我的课看起来像这样:

class Foo {
    const UNKNOWN = 2;

    public function doStuff($var) {
        if($var==UNKNOWN) {
            echo "Unknown state";
            return;
        }
        // other stuff
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我收到此错误doStuff():

使用未定义的常量UNKNOWN - 假设'UNKNOWN'

我究竟做错了什么?我不能定义自定义常量吗?

Joh*_*nde 10

self::在访问班级中的常量时,您必须使用或类名:

if($var == self::UNKNOWN) {
    echo "Unknown state";
    return;
}
Run Code Online (Sandbox Code Playgroud)