: Does this article mainly introduce how PHP can call non-static methods statically? (Weird call). If you are interested in the PHP Tutorial, you can refer to it. 1. Can I call non-static methods statically in PHP?
Today I was asked if the className: methodName () method can be used in PHP to call a method without static declaration. In my impression, I seem to have seen this usage, but I'm not sure. As we all know, in manuals or tutorials, methods are divided into static and non-static methods. generally, static call methods must be static.
What if we call a non-static method? First, perform the test.
Run the following command to return the following error:
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 call of non-static methods is not feasible, but in fact, the conclusion is too early, because the test () method is special, with the same name as the class, is the constructor. We continue the test.
Execution result:
This indicates that,Static call of non-static methods is feasible, but static call of constructor is not feasible. To verify this conclusion, I conducted the following tests:
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 declare static, so the above inference is correct.
However, this result is indeed very special, because it mayOnly PHP can call non-static methods staticallyI used Java for an experiment. if the static call is not static, the following error will be reported:
1 |
Cannot make a static reference to the non-static method showString() from the type HelloWorldApp |
I did not try other languages one by one, but it is enough to explain the special characteristics of PHP. I have not found any instructions on Why PHP is like this.
2. should static call non-static methods be applied?
Can we use this method instead of the static method?First of all, in terms of code readability, static call of non-static methods is certainly not recommended.This may cause maintenance personnel to be confused.
Next we will conduct some experiments to see if static call of non-static methods has certain efficiency advantages.
6 |
for ( $k =0; $k <10000; $k ++) { |
The execution time of the above code here is 18 to 28 milliseconds. let's test the standard writing method again.
3 |
static function test2(){ } |
6 |
for ( $k =0; $k <10000; $k ++) { |
The execution time of the above code is between 5 to 10 milliseconds. in this case, the efficiency of static call of non-static methods is much lower than that of standard static method calls.Static call of non-static methods is not recommended for efficiency.
I found that WordPress is using such a strange call method:
Class-wp.php line 206-207:
// Substitute the substring matches into the query.
$ Query = addslashes (WP_MatchesMapRegex: apply ($ query, $ matches ));
The static call is used here, but the actual member function is not static.
Note: PHP cannot call non-static attributes statically.
Error:
Can I call non-static methods statically in PHP? (Weird call), including some content, hope to be helpful to friends who are interested in PHP tutorials.