An analysis of the reasons for the higher efficiency of the static member function of PHP _php tutorial

Source: Internet
Author: User
Many PHP developers know that the use of static member functions of the class is higher than the ordinary member functions of the class, this article from the application level analysis of the problem

Here's an example:

Copy the Code code as follows: header (' content-type:text/html; Charset=utf-8 ');
Class xclass{
public static $var 1 = ' 1111111111111111 ';
Public $var 2 = ' aaaaaaaaaaaaa ';
Public Function __construct ()
{
$this-var2 = ' bbbbbbbbbbbbbbbb ';
}
public static function Secho1 ()
{
echo Self:: $var 1. '

';
}
Public Function Secho2 ()
{
Echo $this-var2. ' ';
}
Public Function Secho3 ()
{
Echo ' CCCCCCCCCCCCCC ';
}
}
Xclass:: Secho1 ();
Xclass:: Secho3 ();
echo "------------------------------
";
$XC = new Xclass ();
$XC-Secho1 ();
$XC-Secho2 ();
?>
Looking carefully at the example above, you will find an interesting place where SECHO1 () is defined as a static method and can still be referred to as a dynamic method in a dynamic class's pair instance, while Secho3 () can also be treated as a static member function, from this level it is not difficult to understand why static member functions are faster than dynamic.

Perhaps because of compatibility,PHP class members do not have obvious static and dynamic points, all members are not explicitly stated in the case will be treated as static members in a specific memory area , so call the static member function and call the normal function, as fast.

But calling dynamic classes is different, it is to use this class structure as a sample, in memory to regenerate an object instance, so there is a process, which for the simple class, it may not be anything, but for the complex class it is obvious impact efficiency.

Some people will worry that the use of static methods will not cause memory consumption too much, in fact, from the above analysis can be known that you do not declare static methods, the system will still treat members as static, so for a completely static method of the class and a completely dynamic but not declare the instance object class memory is almost the same, Therefore, for the more direct logic, it is recommended to use static member method directly, of course, some complex or explicit logic, if completely static class is not impossible, but then lose the meaning of the class, if so, why oop, by use, static method is particularly suitable for the MVC pattern of logic class.

http://www.bkjia.com/PHPjc/802209.html www.bkjia.com true http://www.bkjia.com/PHPjc/802209.html techarticle Many PHP developers know that the use of static member functions of the class is more efficient than the ordinary member functions of the class, this article from the application hierarchy analysis of the problem is an example: Replication generation ...

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