PHP self-learning path-Static Method

Source: Internet
Author: User
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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.