6

I understand that static methods have no access to state of instance objects of their class types and hence referencing $this inside them results in an error.But objects can reference static methods using object to member operator ->

$obj->staticMethod();

and can even pass it their state via paramaters.

$para1 = $obj->para1;

$para2 = $obj->para2;

$obj->staticMethod($para1, $para2);

How is this last example possible when statics are resolved in static context. If someone can explain to me the general behaviour of statics in php code. you can even talk about C related concepts if it will help.

2
  • Closely related: What if I call a static method in non-static way? Commented Apr 26, 2017 at 10:53
  • 2
    Think of a static method as a global function that is invoked using a funky syntax. The same about class static members. The only thing that makes them different than global objects (functions, variables) is the visibility specifiers (protected, private).
    – axiac
    Commented Apr 26, 2017 at 11:03

2 Answers 2

2

Since you state that you already understand what static means, I'll skip over that.

However, it may still be good to reference PHP's documentation on the static keyword. In particular the following two alerts are important (and hard to glance over, really).

Caution    In PHP 5, calling non-static methods statically generates an E_STRICT level warning.

And this one (italic emphasis mine).

Warning     In PHP 7, calling non-static methods statically is deprecated, and will generate an E_DEPRECATED warning. Support for calling non-static methods statically may be removed in the future.

So, to cut a long story short: yes, your example will run (for now), because the PHP interpreter will try to fix up your mistake for you. You should however never do this. What the PHP interpreter will do is:

Say your $obj is of type Foo. Then it will read

$obj->staticMethod($para1, $para2);

conclude that staticMethod is static and instead execute

Foo::staticMethod($para1, $para2);

It is of course perfectly fine to pass parameters that are properties of an instance of Foo. It doesn't matter to staticMethod where the parameters come from.


To elaborate a bit more on why this works, while using $this in a static method is not allowed.

You can think of normal methods as static functions that have one extra: they receive an implicit parameter $this. The value of $this is simply the object on which the method is called. Thus, $obj->do($a, $b, $c) is equivalent to calling Foo::do($obj, $a, $b, $c) and naming the first argument of do, $this. This is convenient, because we can now easily define methods that work on an instance of an object without having to explicitly state over and over again that this instance is a parameter of our methods. Great.

Now back to static functions. The only difference with normal methods is that they do not receive this implicit $this parameter. Thus, using $this inside of them is invalid. Not because it is forbidden, but because it does not reference anything. PHP does not (and cannot) have a clue what $this should refer to.

Another way to look at it. Say that our Foo class has two properties: $para1 and $para2, both numbers. Say that you write a method that returns the sum of these numbers. One way is to do this:

public static function sum($para1, $para2) {
  return $para1 + $para2;
}

Great. Works. However, it is annoying to have to call it like this

$sum = Foo::sum($obj->para1, $obj->para2);

So, this is what methods are for!

public function sum(/* implicit $this parameter */) {
  // write looking up the properties once inside the function, instead
  // of having to write it every time we call the function!
  return $this->para1 + $this->para2;
}

// ...

$sum = $obj->sum(); // $obj is passed implicitly as $this

Because static functions do not receive an implicit $this parameter, using $this inside of them is like trying to use $undefined when you have never defined it. Thus, invalid.

4
  • is safe to say that,there is so much to statics than manual can document. Commented Apr 26, 2017 at 11:16
  • @CholthiPaulTtiopic I believe that the manual is fairly complete, but indeed, they could have emphasized this a bit more. Does this answer your question though? I'm happy to elaborate. Commented Apr 26, 2017 at 11:19
  • I still don't understand why php prohibits $this inside static methods while allowing us to still do stuffs like above examples. Commented Apr 26, 2017 at 11:23
  • @CholthiPaulTtiopic Hope my edit clarifies this for you :-) Commented Apr 26, 2017 at 11:34
0

Static means class members in simple terms , A static data member is accessible within a class regardless object is created or not . The static function are also functions dedicated to whole class . Static function works with static data only bit it can sometimes vary . Though statics are class dedicated, you can access them using object. It is allowed in all languages. Why ? Because of feasibility . If an object is not being able to access static members , that is a limitation.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.