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?

1
  • 18
    As of php7 you cannot call non static method statically Commented Feb 15, 2016 at 4:47

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

2 Comments

Now static calling of non-static methods works but is deprecated. Be careful using this syntax for instance methods!
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
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'

3 Comments

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'?
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.
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->
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.

3 Comments

i have no warning when i use first and i'm in php7
Why don't we have $this inside a static function? What's the architectural logic of this? Thanks.
@LucasBustamante: $this refers to the current object. In a static function, there is no current object; the function exists on the class without need for or reference to an object instance of that class.
4

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

Comments

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.

1 Comment

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?
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. :(

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.