php static function


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?

2/18/2013 2:35:29 PM

Accepted Answer

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

12/20/2014 3:01:20 AM

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'

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow