1. Static variables exist only in the function scope. Note: These variables will not be lost after execution (the variable will still remember the original value when the function is called next time ).
To define static variables, you only need to add the static keyword before the variable.
2. A static member is a type variable that can be viewed as an instance of the entire class rather than a class. Unlike General instance variables, static members only retain one variable value, which is valid for all instances (all instances share this member ).
3. static method: the rules of static methods are the same as those of static variables. The static keyword can be used to mark a method as a static method. The static method can be accessed through the class name and scope qualifier (: :) (paamayim nekudow.im.
Java Code
-
- <? PHP
-
- Var_dump ("Hellow word! ");
-
- /**
-
- * Test example
- */
-
- ClassMyobject {
-
- Public Static$ Mystaticvar =0;// Declare a shared static variable mystaticvar and assign the initial value 0
-
- Function mymethod (){
-
- SELF: $ mystaticvar + =2;
- Echo self: $ mystaticvar."\ N";
-
- }
-
- StaticFunction mymethod2 (){
-
- SELF: $ mystaticvar + =2;
-
- Echo self: $ mystaticvar."\ N";
-
- }
-
- }
-
-
- ClassMytherobjectExtendsMyobject {
-
- Public Static$ Mystaticvar =0;// Rewrite mystaticvar
-
- Function mythermethod (){
- Echo parent: $ mystaticvar."\ N";// Parent access class
-
- Echo self: $ mystaticvar."\ N";// Self accesses the current class
-
- }
-
- }
-
-
- $ Instance1 =NewMyobject ();// Instantiate myobject
- $ Instance1-> mymethod ();
-
- $ Instance2 =NewMyobject ();
-
- $ Instance2-> mymethod ();
-
- $ Instane3 =NewMytherobject ();
-
- $ Instane3-> myothermethod ();
-
-
- Echo myobject: $ mymethod2 ();//
-
- ?>