In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how static methods access non-static methods in php". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how static methods access non-static methods in php.
Static methods in PHP can access non-static methods; you can instantiate the object first, and then call the non-static methods in the object. Static calls to non-static methods are OK, but static calls to constructors are not allowed.
This article operating environment: Windows10 system, PHP7.1 version, Dell G3 computer.
Can static methods access non-static methods in php?
What if we call a non-static method? First, do the test.
Execute the following and return the error as follows:
Fatal error: Non-static method test::test () cannot be called statically in / home/ × × / test.phpon line 7 Call Stack: 0.0002 332548 1. {main} () / home/ × × / test.php:0
At this point, you may think that it is impossible to call non-static methods statically, but in fact, it is too early to draw conclusions, because test () is a special method with the same name as the class and is a constructor. Let's keep testing.
Execution result:
It works too
This shows that it is feasible to call non-static methods statically, but it is not possible to call constructors statically. In order to verify this conclusion, I did the following tests:
The results of the execution are as follows:
Fatal error: Constructor test::test () cannot be static in / home/xxx/test.php on line 9
Constructors cannot declare static, so the above inference is correct.
But this result is really special, because only PHP can call non-static methods statically. I have done an experiment with Java, and if I call non-static methods statically, I will report the following error:
Cannot make a static reference to the non-static method showString () from the type HelloWorldApp so far, I believe you have a deeper understanding of "how static methods access non-static methods in php". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 249
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.