Home  >  Article  >  php教程  >  My thoughts are messy. It turns out that non-static methods can also be called (transferred) statically. My thoughts are static.

My thoughts are messy. It turns out that non-static methods can also be called (transferred) statically. My thoughts are static.

WBOY
WBOYOriginal
2016-07-06 14:25:161097browse

My thoughts are messy. It turns out that non-static methods can also be called statically (transferred). My thoughts are static

1. Non-static methods can be called statically in PHP Static method?

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'm 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 a test first.
<?php<code><?php<br> class test{<br> function test(){<br> echo 'it works';<br> }<br> }<br> test::test(); class test{
function test(){
echo 'it works';
} }Fatal error: Non-static method test::test() cannot be called statically in /home/×××/test.php<br> on line 7 Call Stack: 0.0002 332548 1. {main}() /home/×××/test.php:0 test::test();
Execute the following and the error returned is as follows

Fatal error: Non-static method test::test() cannot be called statically in /home/×××/test.php<span> on line 7 Call Stack: 0.0002 332548 1. {main}() /home/×××/test.php:0</span>
At this time, you may think that calling non-static methods statically is not feasible, but in fact, it is too early to draw a conclusion, because the test() method is quite special. It has the same name as the class and is a constructor method. <?php<br> class test{<br> function test(){<br> echo 'it works';<br> }<br> function test2(){<br> echo 'it works too';<br> }<br> }<br> test::test2();
Let’s continue testing.
it works too
<?php<strong> class test{</strong> function test(){<br> echo 'it works';
} function test2(){<?php<br> class test{<br> static function test(){<br> echo 'it works';<br> }<br> }<br> test::test(); echo 'it works too';
}
}Fatal error: Constructor test::test() cannot be static in /home/xxx/test.php on line 9 test::test2();
Execution result:

This shows that it is possible to statically call non-static methods, but it is not possible to statically call constructors . In order to verify this conclusion, I did the following test:
Cannot make a static reference to the non-static method showString() from the type HelloWorldApp<?php<br> class test{
static function test(){

echo 'it works';

} } test::test(); The execution result is as follows: Constructors cannot be declared static, so the above inference is correct.

But this result is indeed very special, because maybeonly 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:

I have not tried other languages ​​​​one by one, but this is enough to illustrate the special features of PHP. I have not found any relevant explanation as to why PHP has such a situation.
<?php<br> class test{function test2(){}}
for($k=0; $k<10000; $k )
{
test::test2();
}


2. Should static calls to non-static methods be applied?
<?php<br> class test{static function test2(){}}
for($k=0; $k<10000; $k )
{
test::test2();
}

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. <?php class test{function test2(){}} for($k=0; $k<10000; $k ) { test::test2(); } The execution time of the above code here is 18 to 28 milliseconds. Let's test the standard writing method. <?php class test{static function test2(){}} for($k=0; $k<10000; $k ) { test::test2(); } The above code execution time is between 5 and 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. .
Statement:
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