PHP static variable usage, PHP static variable access method

Source: Internet
Author: User
  1. Class child{

  2. P Ublic $name;

  3. //define and initialize a static variable $nums reference link: PHP static variable initialization
  4. public static $nums =0;
  5. function __construct ($name) {

  6. $this->name= $name;

  7. }

  8. Public Function join_game () {

  9. //self:: $nums use static variable

  10. self:: $n Ums+=1;

  11. Echo $this->name. " Added snowmen game ";

  12. }
  13. }

  14. //Create three children

  15. $child 1=new Child ("Li Kui");
  16. $child 1->join_game ();
  17. $child 2=new Child ("Zhang Fei");
  18. $child 2->join_game ();
  19. $child 3=new Child ("Tang priest");
  20. $child 3->join_game ();

  21. //See how many people are playing the game

  22. echo "
    has this". Child:: $nums;

copy code
  • Related Article

    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.