Static (statically) calls to non-static methods in PHP

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.

<?phpclass 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/xxx/test.phpon line 7 call stack:0.0002 332548 1. {main} ()/home/xxx/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.

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

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

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

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

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

<?phpclass test{var $id = ' 123 '; function test2 () {echo ' it works ';}} echo Test:: $id;? >

Error:

Fatal error:access to undeclared static property:test:: $id in d:phpxampphtdocsest.php on line 8

Static (statically) calls to non-static methods 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.