Why PHP static member functions are more efficient _ PHP Tutorial

Source: Internet
Author: User
The reason why PHP's static member functions are more efficient is analyzed. Many php developers know that the efficiency of using static member functions of a class is higher than that of common member functions of the class. This article analyzes the problem at the application level. The following is an example: many php developers know that the efficiency of using static member functions of a class is higher than that of common member functions of the class. This article analyzes this problem at the application level.

The following is an example:

The code is as follows:

Header ('content-Type: text/html; charset = utf-8 ');
Class xclass {
Public static $ var1 = '20140901 ';
Public $ var2 = 'aaaaaaaaaaaaa ';
Public function _ construct ()
{
$ This-> var2 = 'bbbbbbbbbbbbbbbbbbbb ';
}
Public static function secho1 ()
{
Echo self: $ var1 .'

';
}
Public function secho2 ()
{
Echo $ this-> var2 .' ';
}
Public function secho3 ()
{
Echo 'cccccccccccccccc ';
}
}
Xclass: secho1 ();
Xclass: secho3 ();
Echo "------------------------------
";
$ Xc = new xclass ();
$ Xc-> secho1 ();
$ Xc-> secho2 ();
?>


Taking the above example seriously, we will find an interesting place. after secho1 () is defined as a static method, it can still be referenced as a dynamic method in the dynamic class object instance, secho3 () can also be used as a static member function. at this level, it is not difficult to understand why static member functions are faster than dynamic ones.

It may be because of compatibility,Php class members do not have obvious dynamic and static values. all members are stored in a specific memory zone as static members without explicit declarations.So calling static member functions is just as fast as calling common functions.

However, dynamic classes are called differently. it uses this class structure as a sample to regenerate an object instance in the memory. Therefore, there is an additional process. for simple classes, it may be nothing, but for complex classes, this has a significant impact on efficiency.

Some people may worry that using static methods will cause excessive memory usage. In fact, we can see from the above analysis that, if you do not declare static methods, the system will still treat members as static, therefore, for a completely static method class and a class that is completely dynamic but does not declare the instance object, the memory usage is almost the same, so for a more direct logic, we recommend that you directly use the static member method. of course, some complicated or obvious logic to the image. if you use a static class completely, it is not impossible, but it will lose the meaning of the class, in this case, why OOP, by purpose, and static methods are particularly applicable to logic classes in the MVC mode.

Using static member functions of the class is more efficient than using common member functions of the class. This article analyzes the problem at the application level. The following is an example: copy generation...

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.