class Exam { public $foo = 1; public static function increaseFoo(){ $this->foo++; echo $this->foo; } } Exam::increaseFoo();
This code generate an Error
E_ERROR : type 1 -- Using $this when not in object context -- at line 5
Is that possible to use global variable into static mathod?
Advertisement
Answer
replace $this
with self
, also you must mark your variable as static when using it in a static method:
class Exam { public static $foo = 1; public static function increaseFoo(){ self::$foo++; echo self::$foo; } } Exam::increaseFoo();