Static Methods static methods are also class methods. Static Methods belong to all object instances. The form is as follows: access modifier static method name () {} note: non-static attributes (variables) cannot be accessed in static class methods ). Inside the class: Class Name: class method name or object name-> class method name inside the class: Class Name: class method name or self: class method name case:
<? PHP // use class student {public static $ struct = 0; Public $ name; // construct ($ name) for static methods) {$ this-> name = $ name; echo "initialization variable <br/>";} public static function enterschool ($ IFEE) {self :: $ response + = $ IFEE;} public static function getask() {// return self: $ response; return student ::$ response ;} // The following statement is incorrect. The static method can only operate the static variable public static function test () {echo $ this-> name ;}} // create student $ stu1 = new student ("A Hui"); // call the static method: // 1 call the method directly by class name. // Student: enterschool (340); // 2 call $ stu1-> enterschool (340) through an object; $ stu2 = new student ("Pepe"); Student :: enterschool (30); echo "total tuition = ". student: getask (). "| ". $ stu2-> getparts (). "<br/>"; Student: Test (); // The error is as follows: // fatal error: using $ this when not in object context in/var/myphp/class/static. class. PHP on line 21?>In actual programming, we often use static methods to operate static variables.
Static Methods:
1. Use static methods to operate static variables
2. Static methods do not support static variable operation fees.
Note: common member methods can operate both static variables and non-static variables.