When is it necessary to use static methods?

I thought that we usually use the static method because we do not need to create objects. and we can use className::staticFunction to call the static method we found today:

test1.php

 <?php class Foo { static public function helloWorld() { print "Hello world " ; } } Foo::helloWorld(); 

test2.php

 <?php class Foo { public function helloWorld() { print "Hello world " ; } } Foo::helloWorld(); 

Question:

Both of the above scripts work. We did not declare the function as static , we can still use className::staticFunction to call the function. Why do we need static methods?

+4
source share
6 answers

We did not declare the function as static, we can still use className::staticFunction

What you probably haven't noticed is that PHP complains about the second type of call:

Strict PHP standards: the non-static method Foo::helloWorld() should not be called statically in php shell code on line 1

Strict standards: the non-static method Foo::helloWorld() should not be called statically in php shell code on line 1

To make these notifications visible, you need to set the error_reporting value to -1 , either using ini_set() , or via the php.ini configuration file; By the way, this is recommended during development.

Conclusion

A function called statically must be declared as static function xyz() .

Update

Btw, using the scope :: operator does not necessarily mean that you are making a static call; consider this example:

 class Foo { public function helloWorld() { print "Hello world "; } public function doSomething() { self::helloWorld(); } } $f = new Foo; $f->doSomething(); 

This works because using self:: unlike Foo:: does not change the call mode (unless the method you are calling is defined as static ).

+5
source

The “problem” with static methods is what they are called:

 Foo::bar(); 

Any call to the static method, as necessary, is hard-coded and cannot be easily replaced. Compare with:

 $foo->bar(); 

$foo is a variable here, that is, the exact object and implementation of bar() can be replaced. This is important for the basics of dependency injection.

You are using a static method to:

  • first of all, cases when you do not need separate instances of objects
  • all you need to do before an object can be created
  • alternative object constructors, for example DateTime::createFromFormat() instead of new DateTime
  • The idempotent features that you are 100% sure should never be replaced or mocked.

You can use static functions in other scenarios here and there, but these are the main points. You should know that declaring a static method means that you need to call it statically, which means that its use of talk time cannot be changed. For a long contract on this subject, read How not to kill your test ability with statics .

+2
source

From the above example test1.php

helloworld() The function cannot be overridden or overloaded since you added a static keyword.

However, in the second example test2.php

helloworld() function can be overloaded and overridden

Illustration: 1 (works)

 <?php class Foo { function helloWorld() { print "Hello world " ; } } class Foo1 extends Foo { function helloWorld() { echo "Foo World"; } } $Foo1 = new Foo1(); $Foo1->helloWorld(); //Foo World 

Figure: 2 (Fails)

Unable to make the static method Foo :: helloWorld () not static

 <?php class Foo { static function helloWorld() { print "Hello world " ; } } class Foo1 extends Foo { function helloWorld() { echo "Foo World"; } } $Foo1 = new Foo1(); $Foo1->helloWorld(); 
0
source

Well, just a hello world program may not be able to show a big difference, it's using static vs, but not looking at this class

 class foo { private $a = 1; private static $b = 2; public function foobar() { echo $this->a; } } 

in this above class, if you call foobar statically then $this->a will not resolve.

0
source

PHP is so funny, but usually I have a utility class that takes arguments of things to execute logic and return. Such material does not need an instance of the class. It is for the user / developer to call the methods correctly (read: use the correct accessor access methods).

0
source

When you are working on a large project based on OOP, you will undoubtedly work with many classes (both parent and child classes). The unfortunate consequence of this is that to access elements from different classes, they must be passed manually through each class (or, even worse, store the instance in a global variable). This can be painstakingly frustrating and can lead to confusing code and poor project design. Fortunately, static elements are accessible from any context (i.e., anywhere in your script), so you can access these methods without passing a class instance from object to object.

Since you do not need to declare an instance of an object to access static elements, you can be saved from unnecessary declarations to access seemingly simple functions.

Static elements are available in every instance of the class, so you can set values ​​that should be available to all members of the type.

0
source

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


All Articles