can a non-static method be called statically in 1.PHP?
Today I was asked if PHP could use the classname::methodname () method to invoke a method that does not declare static. In my impression, I seem to have seen this usage, but some are not sure. As we all know, in manuals or tutorials, methods are divided into static and non-static methods, and often we statically invoke methods, which are definitely static methods.
What happens if we call a non-static method? First do the test.
Perform the following and return an error 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 point, you may think that static calls to non-static methods are not feasible, but in fact, the conclusion of the premature, because the test () This method is more special, and the same name of the class, is the construction method. Let's continue the test.
Execution Result:
This means that static calls to non-static methods are possible, but static invocation of the constructor method is not possible . To verify this conclusion, I have done the following tests:
The results of the implementation are as follows:
1 |
Fatal error: Constructor test::test() cannot be static in /home/xxx/test.php on line 9 |
The construction method cannot declare static, so the above inference is correct.
But this result is really special, because it is possible that only PHP can statically invoke non-static methods , I experimented with Java, if static call non-static method will report the following error:
1 |
Cannot make a static reference to the non-static method showString() from the type HelloWorldApp |
Other languages I don't have one by one attempts, but that's enough to explain what's special about PHP, and I can't find a description of why PHP has this situation.
2. Should static calls to non-static methods be applied?
Can we use this method instead of the static method? first of all, in terms of readability of the code, static calls to non-static methods are certainly not recommended , which can cause doubts for defenders.
Next, let's do some experiments to see if static calls to non-static methods have a certain advantage in terms of efficiency.
6 |
for ( $k =0; $k <10000; $k ++) { |
The above code in my execution time is 18 to 28 milliseconds, we will test the standard wording.
3 |
static function test2(){ } |
6 |
for ( $k =0; $k <10000; $k ++) { |
The above code executes between 5 and 10 milliseconds, so it seems that static calls to non-static methods are much less efficient than standard static method calls, so static calls to non-static methods are not recommended for efficiency .
Found in WordPress actually in such a strange way to call:
class-wp.php 第 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 statically invoke non-static properties
Error:
The above is introduced in PHP can statically invoke non-static method? (weird call), including the content of the aspect, I hope to be interested in PHP tutorial friends helpful.