A brief analysis of the reasons for the higher efficiency of static member functions in PHP _php skills

Source: Internet
Author: User
Tags php class

Many PHP developers know that using the static member function of a class is more efficient than the normal member function of a class, this article analyzes this problem from the application level

Here is an example:

Copy Code code as follows:
<?php//php static method test
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 "------------------------------<br/>";
$XC = new Xclass ();
$XC-> secho1 ();
$XC-> Secho2 ();
?>

If you look at the example above, you will find an interesting place, secho1 () is defined as a static method and can still be referenced as a dynamic method in the instance of a dynamic class, and 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.

Probably because of the compatibility reason, thePHP class member actually does not have the obvious static and dynamic state the point, all member in does not have the explicit declaration case will be treated as the static member in the specific memory area , therefore calls the static member function to call the ordinary function to be same, the speed is very fast.

The invocation of a dynamic class, however, is different, and it takes this class structure as a sample to regenerate an object instance in memory, so there is a more process, which may not be much for a simple class, but it is obviously effective for a complex class.

Some people worry that using static methods will not cause excessive memory consumption, in fact, from the above analysis you can know that you do not declare static method, the system will still regard the member as static, so for a completely static method of class and a completely dynamic but not declared instance object of the class occupy memory almost the same, So for the more direct logic, it is recommended to use the static member method directly, of course, some complex or explicit logic, if the full use of static classes is not impossible, but that would lose the meaning of the class, if so, why oop, by Purpose, static method is particularly applicable to the MVC pattern of the logical class.

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.