Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Access to Undeclared Static Property

I've made a class in PHP and I'm getting a Fatal Error(Title) on the line marked with an asterisk(*)

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;
    }
}

I can't figure out what's causing it, also, the following class(same file) is returning the same error.

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, $b, $c, $d, $e, $f){
        self::$id=$a;
        self::$name=$b;
        self::$type=$c;
        self::$ability=$d;
        self::$desc=$e;
        self::$cost = $f;
    }
}

Do you happen to know what's causing the error or how I can fix it?

like image 525
Yaakov Schectman Avatar asked Dec 27 '14 18:12

Yaakov Schectman


2 Answers

You should declare your properties with keyword static, e.g.

private static $id = 0;
like image 188
Dmitri Kadykov Avatar answered Oct 19 '22 10:10

Dmitri Kadykov


Use $this-> instead of self::

Self is for static members and $this is for instance variables.

like image 23
Ryan Printup Avatar answered Oct 19 '22 08:10

Ryan Printup