致命错误:访问未声明的静态属性:StaticMethod :: $ a

时间:2015-12-28 18:44:31

标签: php

class StaticMethod
{

public $a=10;

public $b=20;

    public static function sum(){
        return (self::$a+self::$b);
    }
}

echo StaticMethod::sum();

1 个答案:

答案 0 :(得分:0)

您已将sum方法声明为静态,但您未将变量$a$b声明为静态。

class StaticMethod
{
    public static $a = 10;
    public static $b = 20;

    public static function sum() {
        return (self::$a + self::$b);
    }
}

echo StaticMethod::sum(); //returns 30