PHP访问未声明的静态属性

前端之家收集整理的这篇文章主要介绍了PHP访问未声明的静态属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我用 PHP编写了一个类,我在标有星号(*)的行上遇到致命错误(标题)
class monster{
    private $id = 0;
    private $name = "";
    private $baseLevel = 0;
    private $attack = 0;
    private $defense = 0;
    private $baseEXP = 0;
    private $dropType = 0;
    private $dropNum = 0;
    function __construct($a,$b,$c,$d,$e,$f,$g,$h){
    *   self::$id=$a;
        self::$name = $b;
        self::$baseLevel = $c;
        self::$attack = $d;
        self::$defense = $e;
        self::$baseEXP = $f;
        self::$dropType = $g;
        self::$dropNum = $h;
    }
}

我无法弄清楚导致它的原因,以下类(同一文件)返回相同的错误.

class item{
    private $id = 0;
    private $name = "";
    private $type = 0; #0-weapon,1-armor,2-charm,3-ability
    private $ability = 0;
    private $desc = "";
    private $cost = 0;
    function __construct($a,$f){
        self::$id=$a;
        self::$name=$b;
        self::$type=$c;
        self::$ability=$d;
        self::$desc=$e;
        self::$cost = $f;
    }
}

你碰巧知道是什么导致错误或我如何解决它?

您应该使用关键字static声明您的属性,例如
static private $id = 0;

猜你在找的PHP相关文章