The influence of static keywords on variables and functions in php. In php, static is a static variable. it can define a function. the variable is a global static variable, in this case, if we add static before a function or variable, what will be generated for the function and variable? in php, static is a static variable, which can be defined as a function and the variable is a global static variable, so how does adding static before a function or variable affect the function and variable? let's take a look at it.
1) the description of global variables (external variables) is preceded by static to form a static global variable. Global variables are static storage, and static global variables are also static storage. The two are not different in storage methods. The difference between the two lies in that the scope of non-static global variables is the entire source program. when a source program is composed of multiple source files, non-static global variables are valid in each source file. The static global variable limits its scope, that is, it is valid only in the source file defining the variable, and cannot be used in other source files of the same source program. Because the scope of static global variables is limited to one source file, they can only be shared by functions in the source file. Therefore, errors in other source files can be avoided.
2) from the above analysis, we can see that after a local variable is changed to a static variable, its storage mode is changed, that is, its survival time is changed. After changing a global variable to a static variable, it changes its scope and limits its scope of use.
3) static functions and common functions have different scopes. they are only available in this file. Only functions used in the current source file should be declared as internal functions (static). internal functions should be described and defined in the current source file. For functions that can be used outside the current source file, it should be described in a header file that the source file to use these functions must contain this header file.
After PHP5.3.0, we can use a variable to dynamically call the class. However, the value of this variable cannot be self, parent, or static.
Example #1 static member code Example
The code is as follows: |
|
Class Foo { Public static $ my_static = 'foo ';
Public function staticValue (){ Return self: $ my_static; } } Class Bar extends Foo { Public function fooStatic (){ Return parent: $ my_static; } } Print Foo: $ my_static ."";
$ Foo = new Foo (); Print $ foo-> staticValue ().""; Print $ foo-> my_static. ""; // Undefined "Property" my_static Print $ foo: $ my_static .""; $ Classname = 'foo '; Print $ classname: $ my_static. "; // can be called dynamically after PHP 5.3.0 Print Bar: $ my_static .""; $ Bar = new Bar (); Print $ bar-> fooStatic ().""; ?> Example #2 static method code Example Class Foo { Public static function aStaticMethod (){ //... } } Foo: aStaticMethod (); $ Classname = 'foo '; $ Classname: aStaticMethod (); // As of PHP 5.3.0 ?> |
The PHP Manual provides the following conventions about the use of Static keywords in a class:
1. if the declared class member or method is static, you can directly access it without instantiating the class. You cannot access static members through an object (except for static methods ).
2. because static methods can be called without passing through objects, the pseudo variable $ this is not available in static methods.
3. static attributes cannot be accessed by objects through the-> operator.
4. use: to call a non-static method may cause an E_STRICT error.
Now let's take a look at the 4th conventions.
Running environment: (Win32) PHP/5.3.3
The code is as follows: |
|
Class Foo { Public static $ my_static = 'foo'; // declare a static member Public function staticValue () {// static method Return self: $ my_static ;// } Public function run () {// non-static method Return "abc "; } Public function callrun (){ Return self: run (); // use self: To call a non-static method } } Echo Foo: $ my_static ." "; Echo Foo: run (); // use className: method name to call a non-static method Echo Foo: callrun (); |
Static keywords for interview questions
Question code: write the calculation result of the following code ()
The code is as follows: |
|
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; } |
At first glance, we thought it was a simple self-increment calculation: We thought the result was 400, and the answer to the result was incorrect. Finally, I carefully read the code and learned that the code is 500. $ I + = $ n is printed twice. before calculation, $ I is 100 at a time and 200 at a time. it is known that the problem is static. Then Baidu suddenly realized the static keyword.
Static Keyword:
PHP uses static variables in a wider range. we can not only add static modifiers before classes, methods, or variables, but also add static keywords to internal variables of the function. The variable with the static modifier is not lost even after the function is executed. that is to say, the variable still remembers the original value when the function is called the next time. For example:
The code is as follows: |
|
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 result is as follows: 3 5 7 |
To sum up:
What is the difference between static global variables and common global variables:
Static global variables are modified only once to prevent being referenced in other file units;
What is the difference between static local variables and common local variables:
Static local variables are initialized only once, and the next time is based on the previous result value;
What are the differences between static functions and common functions:
The static function has only one copy in the memory, and the normal function maintains one copy in each call.
...