Talk about static variables in PHP. Static variables only exist in the function scope. that is to say, static variables only exist in the stack. Generally, variables in a function are released after the function ends, such as local variables, but static variables only exist in the function scope. that is to say, static variables only survive in the stack. Generally, variables in a function are released after the function ends, such as local variables, but static variables do not. That is to say, when you call this function again, the value of this variable will be retained.
As long as the keyword static is added before the variable, the variable becomes a static variable.
";}// First execution, $ nm = 2 test (); // first execution, $ nm = 4 test (); // first execution, $ nm = 8 test ();?>
Program running result:
248
After the test () function is executed, the values of the variable $ nm are saved.
Static attributes are often used in class, such as static members and static methods.
Program List: static member of the class
The static variable $ nm belongs to bkjia but not to an instance of the class. This variable is valid for all instances.
: It is a scope-limited operator. here, the self scope is used, instead of $ this scope. $ this scope only indicates the current instance of the class, and self: indicates the class itself.
';}}$nmInstance1 = new bkjia();$nmInstance1 -> nmMethod();$nmInstance2 = new bkjia();$nmInstance2 -> nmMethod();?>
Program running result:
35
Program List: Static attribute
"; // Call the class method $ bkjia = new NowaMagic (); print $ bkjia-> n1_hod ()."
"; Print Article: $ nm ."
"; $ NmArticle = new Article (); print $ nmArticle-> n1_hod ()."
";?>
Program running result:
www.bkjia.comwww.bkjia.comwww.bkjia.comwww.bkjia.com
Program List: a simple static constructor
PHP does not have a static constructor. you may need to initialize a static class. there is a very simple method that directly calls the Demonstration () method of the class after the class definition.
Program running result:
This is the result of demonstration()
Bytes. The variable in a common function will be released after the function ends, for example, a local variable, but static variable...