PHP static variables and static methods

Source: Internet
Author: User

PHP Static variables
Static variables (also called class variables)

Basic usage of static variables
1. Define the static variable form in the class:
[Access modifier] static $ variable name;
Cases public static $nums = 0;
2. How to access static variables
If there are two methods to access in a class self::$ static variable name, class Name:: $ static variable name
If you are accessing outside the class: There is a method class name:: $ static variable name

Routines:
Ask a question:
said that a group of children were playing snowman, and there were new children to join,
How do I know how many people are playing now? Please use the
object to the idea of writing programs to solve

[PHP] View PlainCopy<?PHPclassChild { Public $name; //this defines and initializes a static variable $nums     Public Static $nums=0;  Public Static $i=1; //constructor Function    function__construct ($name)      {                                $this->name=$name; }       //Static Methods     Public  functionJoin_game () {//Self :: $nums use static variable//self:: $nums +=1; orChild::$nums+=1; EchoChild::$i.".".$this->name. " Join the snowmen game! <br> "; Child::$i++; }  }  //Create three children$child 1=NewChild ("Li Kui"); $child 1-Join_game (); $child 2=NewChild ("Zhang Fei"); $child 2-Join_game (); $child 3=NewChild ("Tang Monk"); $child 3-Join_game (); $child 4=NewChild ("Eight commandments"); $child 4-Join_game (); //See how many people are in the game.Echo"<br/> Total--". Child::$nums." --individual participated in the game! "; ?>

Static methods (also called class methods)
The form is as follows:
[Access modifier] static function method name () {}
Cases public static function Enter_school ($ifree) {}
Requirements: When we operate on static variables, we can consider using static methods, such as counting the tuition fees that all students pay.

In our programming, we often use static methods to manipulate static variables.

You do not need to create an object using a static method, you can access the static method directly

2. How to access the static method:
If there are two ways to access in a class self:: Static method Name, class Name:: Static method Name
If you access outside of the class: There are two methods for the class name:: Static method Name, object name, class method name
Characteristics of static methods
1. Static methods can only manipulate static variables
2. Static methods cannot manipulate non-static variables.
Note here: The normal member method can either manipulate non-static variables or manipulate static variables */

<?PHPclassStudent { Public $name; //this defines and initializes a static variable $nums     Public Static $free=0; //Public static $i = 1; constructor function    function__construct ($name,$ifree)      {                                $this->name=$name; Echo"<br>"; Echo $this->name. " Enrolled, to pay tuition: ".$ifree." Yuan <br> "; }       //static method, freshman admission, tuition fee     Public Static functionEnter_school ($ifree) { self::$free+=$ifree; }      //Get Tuition     Public Static functionGetfree () {returnSelf::$free; }     }  //static methods do not need to create objects like normal member methods to be called, but static methods can be called without creating an object//student::enter_school (10000); Create Student Objects$student 1=NewStudent ("Xiao Ming", 1000); //calling static methods by object name$student 1->enter_school (1000); //The static method is called by the class name//student::enter_school (1000); $student 2=NewStudent ("Little East", 200); $student 2->enter_school (2000); $student 3=NewStudent ("Xiao Liang", 3000); $student 3->enter_school (3000); Echo"Total tuition fees".$student 3->getfree (). " Meta!<br> "; //echo "Total tuition fee". Student::getfree (). "  Yuan!<br> "; ?>

PHP static variables and static methods

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.