0

我有一个带有静态变量和 get 函数的类的简单案例,所有编译都可以,但是在运行时我收到了这个错误

[Sun Jul 25 03:57:07 2010] [error] [client 127.0.0.1] PHP Fatal error:  Undefined class constant 'TYPE' in .....

对于函数 getType()

这是我的课

class NoSuchRequestHandler implements Handler{
    public static $TYPE  = 2001;
    public static $VER   = 0;

    public function getType(){
      return self::TYPE;
    }

    public function getVersion(){
      return self::VER;
    }
}

谢谢你们

4

3 回答 3

6

PHP 认为您正在尝试访问类常量,因为:

return self::TYPE;

http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php

正如克里斯提到的,使用:

return self::$TYPE;
于 2010-07-25T01:17:44.280 回答
5

您可以通过这两种方式访问​​它,因为它是公开的...

class NoSuchRequestHandler implements Handler{
    public static $TYPE  = 2001;
    public static $VER   = 0;

    public function getType(){
        return self::$TYPE;  //not the "$" you were missing.  
    }

    public function getVersion(){
        return self::$VER;
    }
}

echo NoSuchRequestHandler::$TYPE; //outside of the class.
于 2010-07-25T01:09:58.417 回答
0

可能令人困惑的问题是关于非静态类的变量

$myClass->anyVar //here there is no $ character for class variable

但对于静态类

MYCLASS::$anyVar  // you must use the $ char for class variable
于 2019-08-24T02:13:40.077 回答