Can PHP call non-static methods statically? (Weird call)

Source: Internet
Author: User
: Does this article mainly introduce how PHP can call non-static methods statically? (Weird call). If you are interested in the PHP Tutorial, you can refer to it. 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? First, perform the test.

1

2 classtest{

3 functiontest() {

4 echo'it works';

5 }

6 }

7 test::test();

8 ?>

Run the following command to return the following error:

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

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

1

2 classtest{

3 staticfunctiontest() {

4 echo'it works';

5 }

6 }

7 test::test();

8 ?>

The execution result is as follows:

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

1 Cannot make a static reference to the non-static method showString() 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.

1

2 classtest{

3 functiontest2() { }

4 }

5

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

7 test::test2();

8 }

9 ?>

The execution time of the above code here is 18 to 28 milliseconds. let's test the standard writing method again.

1

2 classtest{

3 staticfunctiontest2(){ }

4 }

5

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

7 test::test2();

8 }

9 ?>

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.

I found that WordPress is using such a strange call method:

Class-wp.php line 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 call non-static attributes statically.

1

2 classtest{

3 var$id= '123';

4 functiontest2() {

5 echo'it works';

6 }

7 }

8 echotest::$id;

9 ?>

Error:

1 Fatal error: Access

Can I call non-static methods statically in PHP? (Weird call), including some content, hope to be helpful to friends who are interested in PHP tutorials.

Related Article

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.