


Introduction to the reasons why PHP uses the static method (code example)
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
<?php class staticTest { public function test() { $i = 0; $i++; }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...
