php static function php static function php php

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'


Entire difference is, you don't get $this supplied inside the static function. If you try to use $this, you'll get a Fatal error: Using $this when not in object context.

Well, okay, one other difference: an E_STRICT warning is generated by your first example.