Learn PHP, encounter such a problem class mypc{static public $name = "My Computer"; Function Show () {echo $this->name. " Powering on ... "; }} normally: $p = new MyPC (); $p->show (); This is no problem, but it can also be called to Show Method Mypc::show (); PHP did not make an error, so the question arose. We all know that static classes can be invoked in the same way as the class name. Method name, and in PHP, the explanation for:: is also a static member in the calling class. But the show method does not have a static keyword, which means it is not a static method of "explicit" declarations. Please note that this "display". In. NET, if there is no static keyword, the class name is not available. This method, in PHP, does not seem to have this limitation, so that after use, it seems that the Show method is automatically invoked as a static method. So, if you don't consider the attribute problem, the whole class is a method, then I have to go to the new class. Asked for a long time in PHP100 's forum, there is no particularly accurate answer, let alone that: Php automatically converted to static methods, you do not go to the new object and open up a novel memory address, you can directly use the method of the class, the method will also be a member of the class property and exist, Does not occupy separate memory because of the new object. In this case, the potential danger is: if the variable $name static removed will be an error. The properties in the method must also be static in order to be invoked. There is this is not a static property is not called to, such as removing $name static, MyPC:: $name is not. The benefits, of course, are obvious, and the ability to automatically convert to static methods reduces memory loss.
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