Home >Backend Development >PHP Tutorial >PHP static variable usage, PHP static variable access method

PHP static variable usage, PHP static variable access method

WBOY
WBOYOriginal
2016-07-25 08:52:44995browse
  1. class Child{

  2. public $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(){< ;/p>

  9. //self::$nums Use static variables

  10. self::$nums+=1;

  11. echo $this->name."Join making a snowman Game";

  12. }
  13. }

  14. //Create three children

  15. $child1=new Child("李逵");
  16. $child1->join_game();
  17. $child2=new Child("Zhang Fei");
  18. $child2->join_game();
  19. $child3=new Child("Tang Monk");
  20. $child3->join_game();

  21. //Look how many people play the game

  22. echo "
    There is this".Child::$nums;

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn