Can I statically invoke non-static methods in PHP? (Weird Call)

Source: Internet
Author: User
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.

1

2 classtest{

3 functiontest() {

4 echo'it works';

5 }

6 }

7 test::test();

8 ?>

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.

01

02 classtest {

03 functiontest() {

04 echo'it works';

05 }

06 functiontest2() {

07 echo'it works too';

08 }

09 }

10 test::test2();

11 ?>

Execution Result:

1 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:

1

2 classtest{

3 staticfunctiontest() {

4 echo'it works';

5 }

6 }

7 test::test();

8 ?>

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.

1

2 classtest{

3 functiontest2() { }

4 }

5

6 for($k=0;$k<10000; $k++) {

7 test::test2();

8 }

9 ?>

The above code in my execution time is 18 to 28 milliseconds, we will test the standard wording.

1

2 classtest{

3 staticfunctiontest2(){ }

4 }

5

6 for($k=0;$k<10000; $k++) {

7 test::test2();

8 }

9 ?>

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

1

2 classtest{

3 var$id= '123';

4 functiontest2() {

5 echo'it works';

6 }

7 }

8 echotest::$id;

9 ?>

Error:

1 Fatal error: Access

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.

  • 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.