"PHP Global variables and static variables, how to use static methods"

Source: Internet
Author: User

The PHP global variable uses the keyword global declaration, static variables use static declarations, and the use of static variables can use the class name:: Variable name

Example code:

1<?PHP2 3 //usage of global variables and use of static variables4     Global $sum;5     $sum=0;6     class Person7     {8          Public $name;9          Public $age;Ten          Public Static $zong=0; One          Public function__construct ($name,$age) A         { -             $this->name=$name; -             $this->age=$age; the         } -          Public functionAddNew () -         { -             Echo $this->name. " Join the team! <br/> "; +             Global $sum; -             $sum++; +Self::$zong++; A         } at          Public function__destruct () -         { -             Echo"<br/>".$this->name. " Leave the team! <br/> "; -         } -     } -     $p 1=NewPerson ("Zhang San", 12); in     $p 1-AddNew (); -     $p 2=NewPerson ("John Doe", 13); to     $p 2-AddNew (); +     Echo"Shared".$sum." People join the team <br/> "; -     Echo"<br/><br/><br/><br/>"; the     Echo"The record value for the static variable is". Person::$zong; *  $     //usage of static variable globalPanax Notoginseng  -?>
View Code

Examples of use of static methods:

1<?PHP2         classStudent3         {4              Public Static $sum=0;5              Public $fee;6              Public $name;7              Public function__construct ($name,$fee)8             {9                 Echo $name." Join the school, spend ".$fee." Yuan <br/> ";Ten                 $this-$fee=$fee; One                 $this-$name=$name; ASelf::$sum+=$fee; -             } -              Public Static functionGetfee () the             { -                 returnSelf::$sum; -             } -         } +         $st 1=NewStudent ("Zhang San", 12); -         $st 2=NewStudent ("John Doe", 13); +         Echo"School total income (class name static method call)". Student::getfee (). " Yuan <br/> "; A         Echo"School total income (instance static method invocation)".$st 1->getfee (). " Yuan <br/> "; at?>
View Code

"PHP Global variables and static variables, how to use 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.