Mind messy, original non-static method can also be static call (go), Thoughts static _php tutorial

Source: Internet
Author: User

Mind messy, original non-static method can also be static call (turn), thoughts static


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.
class test{
function test(){
echo 'it works';
}
}
test::test();

Perform the following and return an error as follows
Fatal error:Non-static method test::test() cannot be called statically in/home/×××/test.php
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.
class test{
function test(){
echo 'it works';
}
function test2(){
echo 'it works too';
}
}
test::test2();

Execution Result:
it works too
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:
class test{
static function test(){
echo 'it works';
}
}
test::test();

The results of the implementation are as follows:
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:
Cannot make a static reference to the non-static methodshowString() 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.
class test{function test2(){}}
for($k=0; $k<10000; $k++)
{
test::test2();
}

The above code in my execution time is 18 to 28 milliseconds, we will test the standard wording.
class test{static function test2(){}}
for($k=0; $k<10000; $k++)
{
test::test2();
}

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 .

http://www.bkjia.com/PHPjc/1135471.html www.bkjia.com true http://www.bkjia.com/PHPjc/1135471.html techarticle thoughts Messy, original non-static method can also be static call (go), thoughts static 1.PHP static method can be called? Today, I was asked if I could use ClassName in PHP: ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.