Home > Backend Development > PHP Tutorial > PHP: Analysis of the difference between static static variables and ordinary variables

PHP: Analysis of the difference between static static variables and ordinary variables

黄舟
Release: 2023-03-12 12:10:01
Original
2034 people have browsed it

1. Add static in front of the variable to form a static variable (static variable).
2. The difference between static variables and ordinary variables 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 in each are valid in the 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. 3. The difference between static variables and ordinary variables:
The difference between static global variables and ordinary global variables: static global variables are only initialized once to prevent them from being
referenced in other file units;The difference between static local variables and ordinary local variables: static local variables are only initialized once, and the next time is based on the previous result value;
The difference between static functions and ordinary functions: there is only one copy of the static function in the memory, and the ordinary function is initialized every time Maintain a copy for each call.
4. Add the keyword static before the global variable, and the global variable is defined as a global static variable.
1) Location in memory: static storage area (static storage area exists during the entire program running)
2) Initialization: Uninitialized global static variables will be automatically initialized to 0 by the program (automatic object The value is arbitrary unless he is explicitly initialized)
3) Scope: Global static variables are not visible outside the file in which they are declared. Exactly from the point of definition to the end of the file.
5. Advantages of static variables:
will not be accessed by other files. Modify
You can use variables with the same name in other files without conflict.
Location in memory: Static storage area
Initialization: Uninitialized global static variables will be automatically initialized to 0 by the program (the value of the automatic object is arbitrary unless he is explicitly initialized)
Scope : The scope is still a local scope. When the function or statement block that defines it ends, the scope ends.

Note: When static is used to modify local variables, it changes the storage location of local variables from the original stack to the static storage area. However, the local static variable is not destroyed after leaving the scope, but still resides in the memory until the end of the program, but we can no longer access it.
When static is used to modify a global variable, it changes the scope of the global variable (not visible outside the file in which it is declared), but does not change its storage location, which is still in the static storage area. .

Static is to define a static object or static variable. We will know the characteristics of the variables or class methods defined by static after reading the relevant examples in this article.

 1.
Create an object$object = new Class(), and then use "->" to call: $object->attribute/function, provided that the variable/method is accessible.  2. Directly call class methods/variables: class::attribute/function, whether static or non-static. But there are prerequisites:
 A. If it is a variable, the variable needs to be accessible.
 B. If it is a method, in addition to the method being accessible, it also needs to meet:
 b1) If it is a static method, there are no special conditions;
 b2) If it is a non-static method, it needs to be changed if it is not used in the method. $this means that non-static variables/methods are not called. Of course, there is no problem with calling static variables/methods.
Then let’s take a look at the difference between using $object->… and using class::…:
1. When using $object->…, you need to execute the
constructor to create Object;  2. Use class::... to call static methods/variables, and there is no need to execute the constructor to create the object;
 3. Use class::... to call non-static methods/variables, and there is no need to execute the constructor to create the object object.
Then the strange thing comes out. Since 2 and 3 are the same, what is the point of the existence of static methods/variables?
 static static
 Declaring a class member or method as static allows direct access without instantiating the class. Static members (except static methods) cannot be accessed through an object. Static members belong to the class and do not belong to any object instance, but object instances of the class can be shared.

The above is the detailed content of PHP: Analysis of the difference between static static variables and ordinary variables. For more information, please follow other related articles on the PHP Chinese website!

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