In php, static is a static variable. It can define functions. The variables are global static variables. So what impact will adding static in front of the function or variable have on the function and the variable? Let’s do it together. have a look.
1) The description of global variables (external variables) is preceded by static to constitute 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 and 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 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. For functions that can be used outside the current source file, they should be stated in a header file. The source files that use these functions must include this header file
After PHP5.3.0, we can use a variable to dynamically call a class. But the value of this variable cannot be the keywords self, parent or static.
Example #1 Static member code example
The code is as follows | Copy code | ||||
|
Regarding the use of the Static keyword in classes, the PHP manual gives the following convention:
1. Declare class members or methods as static so that they can be accessed directly without instantiating the class. Static members (except static methods) cannot be accessed through an object.
2. Since static methods do not need to be called through objects, the pseudo variable $this is not available in static methods.
3. Static properties cannot be accessed by objects through the -> operator.
4. Calling a non-static method using the :: method will cause an E_STRICT level error.
Now let’s focus on Article 4.
Operating environment: (Win32) PHP/5.3.3
The code is as follows | Copy code | ||||||||||||
Public static $my_static = 'foo';//Declare a static member
Public function staticValue() {//Static method
Return "abc Public function callrun() { return self::run();//Use self:: to call a non-static method } }
"; echo Foo::run();//Use className::method name to call non-static method echo Foo::callrun(); |
The code is as follows | Copy code |
<🎜>function dewen(){<🎜> $k=add_number(100); <🎜> $k+=add_number(100); <🎜> printf("%d",$k); <🎜> Return 0;<🎜> }<🎜> <🎜> function add_number($n){<🎜> static $i=100;<🎜> $i+=$n;<🎜> Return $i; <🎜> }<🎜> |
The code is as follows | Copy code |
<🎜>01 <🎜>02 function test() <🎜> <🎜>03 { <🎜> <🎜>04 static $var1 =1; <🎜> <🎜>05 $var1 +=2; <🎜> <🎜>06 echo $var1 . ' ' ; <🎜> <🎜>07 } <🎜> <🎜>08 <🎜> <🎜>09 test(); <🎜> <🎜>10 test(); <🎜> <🎜>11 test(); <🎜> <🎜>12 ?> The running results are as follows: 3 5 7 |
In summary:
What is 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;
What is 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;
What is the difference between static functions and ordinary functions:
Static functions have only one copy in memory, while ordinary functions maintain a copy for each call