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.