Home > Backend Development > PHP Tutorial > A brief analysis of the reasons why PHP's static member functions are more efficient_PHP Tutorial

A brief analysis of the reasons why PHP's static member functions are more efficient_PHP Tutorial

WBOY
Release: 2016-07-13 10:28:16
Original
849 people have browsed it

Many PHP developers know that using static member functions of a class is more efficient than using ordinary member functions of a class. This article analyzes this problem from the application level

Here is an example:

Copy code The code is as follows:
header('Content-Type: text /html; charset=utf-8');
class xclass{
public static $var1 = '1111111111111111';
public $var2 = 'aaaaaaaaaaaaa';
public function __construct()
 {
                $this -> var2 = 'bbbbbbbbbbbbbbbb'; ';
}
public function secho2()
{
echo $this -> var2 . '
';
}
public function secho3()
{
echo 'cccccccccccccc
';
}
}
xclass :: secho1();
xclass :: secho3();
echo " ----------------------------------
";
$xc = new xclass();
$xc -> secho1();
$xc -> secho2();
?>

Looking at the above example carefully, you will find an interesting point, secho1( ) is defined as a static method, it can still be referenced as a dynamic method in the object instance of the dynamic class, and secho3() can also be used as a static member function. From this level, it is not difficult to understand why static member functions are better than dynamic ones. quick.

Perhaps due to compatibility reasons,
There is no obvious distinction between dynamic and static class members in PHP. All members will be treated as static members and stored in a specific memory area unless explicitly declared.
, so calling static member functions is just like calling ordinary functions, very fast.

But calling a dynamic class is different. It uses this class structure as a sample to regenerate an object instance in the memory, so there is an extra process. This may not be a big deal for simple classes, but for complex For classes, this obviously affects efficiency. Some people may worry that using static methods will cause excessive memory usage. In fact, from the above analysis, we can know that if you do not declare static methods, the system will still treat the members as static. Therefore, for a class with a completely static method and a Classes that are completely dynamic but do not declare instance objects occupy almost the same memory, so for more direct logic, it is recommended to use static member methods directly. Of course, for some complex or object-oriented logic, it is not possible to use static classes entirely. It's impossible, but that would lose the meaning of the class. If so, why bother with OOP? According to usage, static methods are especially suitable for logical classes in the MVC pattern.

http://www.bkjia.com/PHPjc/802209.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/802209.htmlTechArticleMany PHP developers know that using static member functions of a class is more efficient than using ordinary member functions of a class. This article analyzes this problem from the application level. Here is an example: Copy generation...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template