110

I have a question regarding static function in php.

let's assume that I have a class

class test {
    public function sayHi() {
        echo 'hi';
    }
}

if I do test::sayHi(); it works without a problem.

class test {
    public static function sayHi() {
        echo 'hi';
    }
}

test::sayHi(); works as well.

What are the differences between first class and second class?

What is special about a static function?

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
Moon
  • 22,195
  • 68
  • 188
  • 269

6 Answers6

157

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).

Makyen
  • 31,849
  • 12
  • 86
  • 121
Jonathan Fingland
  • 56,385
  • 11
  • 85
  • 79
  • 23
    Now static calling of non-static methods works but is deprecated. Be careful using this syntax for instance methods! – Jet May 24 '09 at 12:52
  • 1
    So this why they say it as static function? because there is no multiple instance with dynamic data flow and dynamic output. Just guide me.@chaos – sun Feb 25 '14 at 13:50
22

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'
Lucas Bustamante
  • 15,821
  • 7
  • 92
  • 86
user2132859
  • 405
  • 3
  • 8
  • 9
    Ok I'm not a PHP wizard, but isn't the variable ('hello') that is passed to the static function, explicitly being overwritten with 'Hi'? Meaning that the line print test1::sayHi('hello'); would not return 'hello', but would return 'hi'? – Fnord23 Nov 12 '15 at 22:27
  • 7
    This answer is sloppy (the effect of static functions *can* depend on which class they're in) and the examples don't make the point very clear. – reinierpost May 17 '16 at 22:00
  • Not a very good example. $hi = 'Hi'; overwrites the given argument, and has nothing to do with static declaration. I would fix this like: self::$hi = $hi; and it would work just like the non-static one. You can access the class variables with self:: instead of $this-> – GotBatteries Apr 07 '20 at 06:51
20

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.

chaos
  • 122,029
  • 33
  • 303
  • 309
4

Calling non-static methods statically generates an E_STRICT level warning.

2

In a nutshell, you don't have the object as $this in the second case, as the static method is a function/method of the class not the object instance.

Czimi
  • 2,494
  • 17
  • 14
  • 1
    is static simply how you define class functions? no other fancy business? So you're saying if it had been called "class_method" rather than "static", it would be more semantic? – ahnbizcad May 10 '15 at 06:31
1

After trying examples (PHP 5.3.5), I found that in both cases of defining functions you can't use $this operator to work on class functions. So I couldn't find a difference in them yet. :(

Cosmin
  • 21,216
  • 5
  • 45
  • 60
yogesh
  • 19
  • 1