Many of my friends may be confused about the usage and differences between static variables and static methods in php. I will introduce some understanding of static variables and static methods based on my own experience. In PHP, quiet
Many of my friends may be confused about the usage and differences between static variables and static methods in php. I will introduce some understanding of static variables and static methods based on my own experience.
In PHP, static variables are interpreted as variables that exist in the class scope. they are assigned a value during the first initialization and will not be re-assigned during class initialization, it is mainly used for variable sharing when a class has multiple instances.
Use static variables
Another important feature of the variable range is static variable. static variables only exist in local function domains, but their values are not lost when the program runs out of this scope, take a look at the following example:
Demonstrate examples of static variables,The code is as follows:
-
- Function Test ()
- {
- $ A = 0;
- Echo $;
- $ A ++;
- }
- ?>
This function is useless, because every call sets the value of $ a to 0 and outputs "0". adding $ a ++ to the variable does not work, because once you exit this function, the variable $ a does not exist. to write a counting function that does not lose the value of this count, you need to define the variable $ a as static:
Example of using static variables,The code is as follows:
-
- Function Test ()
- {
- Static $ a = 0;
- Echo $;
- $ A ++;
- }
- ?>
Now, every time you call the Test () function, the $ a value is output and the value is added.
Static variables also provide a method for processing recursive functions. A recursive function is a function that calls itself. Be careful when writing recursive functions, because infinite recursion may occur. Make sure there are sufficient methods to abort recursion. The simple function calculates 10 recursively and uses the static variable $ count to determine when to stop:
Static variables and recursive functions,The code is as follows:
-
- Function Test ()
- {
- Static $ count = 0;
- $ Count ++;
- Echo $ count;
- If ($ count <10 ){
- Test ();
- }
- $ Count --;
- }
- ?>
Note:Static variables can be declared according to the preceding example. if you assign a value to the expression in the declaration, parsing errors will occur.
Declare static variables,The code is as follows:
-
- Function foo (){
- Static $ int = 0; // correct
- Static $ int = 1 + 2; // wrong (as it is an expression)
- Static $ int = sqrt (121); // wrong (as it is an expression too)
- $ Int ++;
- Echo $ int;
- }
- ?>
Static method,The code is as follows:
-
- Class Fruit {
- Public static $ category = "I'm fruit ";
- Static function find ($ class)
- {
- $ Vars = get_class_vars ($ class );
- Echo $ vars ['Category '];
- }
- }
- Class Apple extends Fruit {
- Public static $ category = "I'm Apple ";
- }
- Apple: find ("Apple ");
- ?>
- // Program running result: 1 I'm Apple
Program List: override the base class method,The code for rewriting the base class in a derived class is as follows:
-
- Class Fruit
- {
- Static function Foo ($ class = _ CLASS __)
- {
- Call_user_func (array ($ class, 'color '));
- }
- }
- Class Apple extends Fruit
- {
- Static function Foo ($ class = _ CLASS __)
- {
- Parent: Foo ($ class );
- }
- Static function Color ()
- {
- Echo "Apple's color is red ";
- }
- }
- Apple: Foo (); // This time it works.
- ?>
- // Program running result: Apple's color is red
Program List: use of static arrays
Static and const scopes can be accessed using the: operator. if you want to use the: Operator to access the array, you need to declare the array as static in advance. the code is as follows:
-
- Class Fruit
- {
- Static $ color = array ('color1' => 'red', 'color2' => 'yellow ');
- }
- Class Apple
- {
- Public function _ construct ()
- {
- Var_dump (Fruit: $ color );
- }
- }
- Class Banana
- {
- Public function _ construct ()
- {
- Fruit: $ color = FALSE;
- }
- }
- New Apple (); // prints array (2) {["color1"] => string (3) "red" ["color2"] => string (6) "yellow "}
- Echo' ';
- New Banana ();
- New Apple (); // prints bool (false)
- ?>
Program List: in the Singleton mode again, Static is really cool. The following Program demonstrates how to obtain an existing instance. the code is as follows:
-
- Class Singleton {
- Private static $ instance = null;
- Private $ value = null;
- Private function _ construct ($ value ){
- $ This-> value = $ value;
- }
- Public static function getInstance (){
- If (self: $ instance = null ){
- Echo"
New
";
- Self: $ instance = new Singleton ("values ");
- }
- Else {
- Echo"
Old
";
- }
- Return self: $ instance;
- }
- }
- $ X = Singleton: getInstance ();
- Var_dump ($ x); // returns the new object
- $ Y = Singleton: getInstance ();
- Var_dump ($ y); // returns the existing object
- ?>
For example, static variables and static methodsThe code is as follows:
- Class {
- Static $ I = 10;
- Public function set ($ n)
- {
- Self: $ I = $ n;
- }
- Public function get ()
- {
- Return self: $ I;
- }
- }
- $ A = new ();
- $ A-> set (11 );
- $ A1 = new ();
- Echo $ a1-> get ();
The output is 11. we can see that after Class A is instantiated for the second time, the static variable $ I is still consistent with the value of $ I set after the last instantiation. it is explained in java, in fact, static variables of a class use a memory space in multiple instances. I think this explanation is easier to understand, because static variables and static methods can be used without instantiation, therefore, static variables are initialized after the file is loaded, while static methods are registered. This can be understood, why is the java class entry like this? the code is as follows:
Public static void main (String [] args ){}
I used to think that static methods can be directly used without instantiation, saving the huge overhead of instantiation. Therefore, when using a class method, I prefer direct static calls to avoid instantiation. I have argued with my colleagues many times about this issue. he does not advocate static calling and has the following ideas:
1. instantiation classes are more in line with the idea of object-oriented programming;
2. static call methods do not save much on consumption.
I still stick to my opinion on this issue, but I do not always use static calls. I mainly have the following ideas:
1. static variables are initialized when the file is loaded. Therefore, if there are multiple classes and multiple static variables and methods in the class, it is bound to consume a lot of memory (not verified) when it is not instantiated, so it is not recommended to set a method to static for infrequent access or special needs;
2. for classes with frequent calls, I strongly recommend that you use static and static methods to share both memory space and data. You will find that the base classes of mainstream PHP frameworks are static call methods.