php static function
In the first class, sayHi()
is actually an instance method which you are calling as a static method and you get away with it because sayHi()
never refers to $this
.
Static functions are associated with the class, not an instance of the class. As such, $this
is not available from a static context ($this
isn't pointing to any object).
Simply, static functions function independently of the class where they belong.
$this means, this is an object of this class. It does not apply to static functions.
class test { public function sayHi($hi = "Hi") { $this->hi = $hi; return $this->hi; }}class test1 { public static function sayHi($hi) { $hi = "Hi"; return $hi; }}// Test$mytest = new test();print $mytest->sayHi('hello'); // returns 'hello'print test1::sayHi('hello'); // returns 'Hi'