PHP static methods

I understand that static methods do not have access to the state of instances of objects of their class types and, therefore, references to $this inside them lead to an error. But objects can reference static methods using an object for a member statement ->

 $obj->staticMethod(); 

and can even transfer this state through paramaters.

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

How this last example is possible when statics are allowed in a static context. If someone can explain to me the general behavior of statics in php code. you can even talk about C related concepts if that helps.

+5
source share
2 answers

Since you are claiming that you already understand what static means, I will skip this.

However, it may be useful to refer to the PHP documentation in the static key . In particular, the following two warnings are important (and it's hard to peek, really).

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

And this one (italics mine).

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

So, to shorten the long story: yes, your example will work (for now), because the PHP interpreter will try to fix your mistake for you. However, you will never do it . The PHP interpreter will do the following:

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

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

conclude that staticMethod is static and instead performs

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

Of course, it's great to pass parameters that are properties of the Foo instance. It does not matter staticMethod where the parameters are taken.


More details on why this works when using $this in the static method is not allowed.

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

Now back to the static functions. The only difference from conventional methods is that they do not receive this implicit parameter $this . Thus, using $this inside them is not valid. Not because it is forbidden, but because nothing is said. PHP cannot (and cannot) understand what $this should mean.

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

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

Great. Works. However, it is annoying to call it as follows

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

So, what are the methods 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 

Since static functions do not receive the implicit $this parameter, using $this inside it is like trying to use $undefined when you never defined it. So it’s wrong.

+1
source

Static means class members in simple expressions, static data member < is available inside the class regardless of whether the object is created or not. Static function - These are also functions designed for the entire class. A static function works with static data, only a bit can sometimes change. Although statics is dedicated to the class, you can access it using an object. This is allowed in all languages. What for? Due to feasibility. If the object cannot access static members, this is a limitation.

0
source

Source: https://habr.com/ru/post/1267177/


All Articles