Home>Article>Backend Development> Introduction to the reasons why PHP uses the static method (code example)

Introduction to the reasons why PHP uses the static method (code example)

不言
不言 Original
2018-09-11 14:41:23 1166browse

The content of this article is an introduction to the reasons why PHP uses static methods (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Static methods can be used directly without the class being instantiated.

Static methods are more efficient than instantiation. The disadvantage of static methods is that they are not automatically destroyed, while instantiated ones can be destroyed.

Static methods and static variables always use the same memory after creation, while using instances will create multiple memories.

What is the difference between static global variables and ordinary global variables, static local variables and ordinary local variables, static functions and ordinary functions? The following article will answer you one by one.
(1) If the description of a global variable (external variable) is preceded by static, it constitutes a static global variable. Global variables themselves are static storage methods, and static global variables are of course also static storage methods. There is no difference between the two in the way they are stored. The difference between the two is that the scope of non-static global variables is the entire source program. When a source program consists of multiple source files, non-static global variables are valid in each source file. Static global variables limit their scope, that is, they are only valid within the source file in which the variable is defined, and cannot be used in other source files of the same source program. Since the scope of static global variables is limited to one source file and can only be shared by functions in that source file, errors can be avoided in other source files.

(2) From the above analysis, it can be seen that changing a local variable to a static variable changes its storage method, that is, changes its lifetime. Changing a global variable to a static variable changes its scope and limits its scope of use.

(3) The scope of static functions is different from that of ordinary functions, only in this file. Functions used only in the current source file should be declared as internal functions (static), and internal functions should be described and defined in the current source file. Functions that can be used outside the current source file should be stated in a header file, and the source files that use these functions should include this header file.

If you can define a class method as static, try to define it as static, and its speed will increase by nearly 4 times.

Practice is the only criterion for testing the efficiency of code execution, so I took advantage of some time when I got home from work and did a small test:

First upload the test code (if there is something wrong, please let me know Correction):

Test environment:

/test # php -v PHP 5.4.24 (cli) (built: Jan 19 2014 21:32:15) Copyright (c) 1997-2013 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
/test # uname -a Darwin 192.168.0.101 13.1.0 Darwin Kernel Version 13.1.0: Thu Jan 16 19:40:37 PST 2014; root:xnu-2422.90.20~2/RELEASE_X86_64 x86_64

      
public static function testStatic() { $i = 0; $i++; } } $start = microtime(true); for($i=0;$i<10000000;$i++) { $test = new staticTest(); $test->test(); } echo (microtime(true) - $start) ."\n"; $start = microtime(true); for($i=0;$i<10000000;$i++) { staticTest::testStatic(); } echo microtime(true) - $start;

The execution result of loop execution ten million times is as follows:

/test#php staticTest.php

2.2938342094421

1.1871800422668
The second execution result:

2.303295135498

1.1807670593262

Many executions After this time, it has been around 2.3 and 1.18. It can be seen that although the efficiency improvement mentioned in the article is not as terrible, the performance improvement has indeed been improved by about double.

Therefore, it is still recommended:

If you can define the class method as static, try to define it as static, and its speed will be nearly doubled.

For a normal PHP framework, there are still many classes and methods in it. If we can make static methods, it is better to use static methods, which is more efficient.

Related recommendations:

Introduction to the use of static methods and static variables in PHP classes

PHP Introduction to the use of static variables_PHP tutorial

The above is the detailed content of Introduction to the reasons why PHP uses the static method (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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