Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP static methods

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.

like image 921
Cholthi Paul Ttiopic Avatar asked Apr 26 '17 10:04

Cholthi Paul Ttiopic


People also ask

What are static methods in PHP?

The static keyword is used to declare properties and methods of a class as static. Static properties and methods can be used without creating an instance of the class. The static keyword is also used to declare variables in a function which keep their value after the function has ended.

When should I use static methods in PHP?

When to define static methods ? The static keyword is used in the context of variables and methods that are common to all the objects of the class. Therefore, any logic which can be shared among multiple instances of a class should be extracted and put inside the static method.

Why not use static methods PHP?

Using static methods and variables breaks a lot of the power available to Object-Oriented code. The technical implementation of them is to allow state to be maintained across all instances of a class.

What is difference between static method and non static method in PHP?

Static class contains static variables and static methods whereas instantiated class contains non-static variables and non-static methods. Programs having static classes are hard to test and to extend while programs with non-static classes provide easy testing and extending property.


1 Answers

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.

like image 92
Just a student Avatar answered Oct 01 '22 14:10

Just a student