My thoughts are messy. The original non-static methods can also be called statically .)

Source: Internet
Author: User

My thoughts are messy. In the past, non-static methods can also be called (transferred) statically.

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

Run the following command to return the following error:
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 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.
<?php
class test{
function test(){
echo 'it works';
}
function test2(){
echo 'it works too';
}
}
test::test2();

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

The execution result is as follows:
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:
Cannot make a static reference to the non-static methodshowString() 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.
<?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 again.
<?php
class test{static function test2(){}}
for($k=0; $k<10000; $k++)
{
test::test2();
}

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.

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.