Home > Backend Development > PHP Tutorial > Can non-static methods be called statically in PHP? (weird call)

Can non-static methods be called statically in PHP? (weird call)

WBOY
Release: 2016-07-30 13:30:34
Original
1378 people have browsed it

1. Can non-static methods be called statically in PHP?

Today I was asked whether I can use the className::methodName() method in PHP to call a method that is not declared static. In my mind, I seem to have seen this usage before, but I am a little unsure. As we all know, in manuals or tutorials, methods are divided into static methods and non-static methods. Usually the methods we call statically must be static methods.

What happens if we call a non-static method? Do the test first.

1 <?php

2 classtest{

​​​​​​'it works'
echo;

5}6
​​​​​

}7

test::test();

8

?>

Execute the following, and the error returned is as follows:

1
Fatal error: Non-static method test::test() cannot be called statically in /home/×××/test.php
2

on line 7 Call Stack: 0.0002 332548 1. {main}() /home/×××/test.php:0

At this time, you may think that static calls are non-static The method will not work, but in fact, it is too early to draw the conclusion, because the test() method is quite special. It has the same name as the class and is a constructor method. Let's continue testing.

01
class test {

03

                                                              ​​
echo'it works' ;

test2() {
05                                                                                                                     function

07}
It works too'

09}

10test::test2();

11
?> This shows that statically calls non-static methods It is feasible, but statically calling the constructor method is not allowed. In order to verify this conclusion, I did the following test:

1

<?php2
class

test{

3

static

function
test() {

4

echo'it works';

5

6

}

test::test();
7

8
?></td> <td> <code>

The execution result is as follows:

1 Fatal error: Constructor test::test() cannot be static in /home/xxx/test.php on line 9

The constructor cannot be declared static, so the above inference is correct.

But this result is indeed very special, because maybe only PHP can statically call non-static methods. I did an experiment with Java. If you statically call a non-static method, the following error will be reported:

1 Cannot make a static reference to the non-static method showString() from the type HelloWorldApp

I have not tried other languages ​​one by one, but this is enough to illustrate the specialness of PHP However, I have not found any relevant explanation as to why PHP behaves like this.

2. Should static calls to non-static methods be applied?

So can we use this method instead of the static method? First of all, from the perspective of code readability, statically calling non-static methods is of course not recommended, which will make maintainers confused.

Next, let’s do some experiments to see if statically calling non-static methods has certain advantages in efficiency.

1 <?php

2 classtest{

3 functiontest2() { }

4}

5

6for($k=0; $k<10000; $k++) {

7 test::test2();

8}

9?>

The execution of the above code here The time is 18 to 28 milliseconds. Let's test the standard writing method again.

1 <?php

2 classtest{

3 staticfunctiontest2(){ }

4 }

5

6for($k=0; $k<10000; $k++) {

7 test::test2();

8}

9?>

The above code execution time is 5 to 10 milliseconds. From this point of view, the efficiency of statically calling non-static methods is much lower than standard static method calls, so statically calling non-static methods is not recommended in terms of efficiency.

I found that WordPress actually uses such a weird calling method:

class-wp.php Lines 206-207:
// Substitute the substring matches into the query.$query = addslashes(WP_MatchesMapRegex:: apply($query, $matches));

Static calling is used here, but the actual member function is not static.


Note: PHP cannot statically call non-static properties

1<?php

2class
test{

3
var$id= '123' ;

4​​

functiontest2() {
6
5 }

7}

8test:: ;
echo$id

9
?>
Error: Access

The above introduces whether non-static methods can be called statically in PHP? (Weird call), including aspects of the content, I hope it will be helpful to friends who are interested in PHP tutorials.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template