php中static静态变量的使用

<?php
//声明文件编码格式
header(\"content-type:text/html;charset=utf-8\");
  //创建孩子玩游戏的类
class child{
 //加入游戏的费用,变量初始化
 public static $user_money=0;
 //用户名
 public $user_name;
 //用户的年龄
 public $user_age;
 //创建当前的用户数,并且做初始化
 public static $number=0;
 //创建一个构造函数进行声明
 function __construct($user_name){
  $this->user_name=$user_name;
  echo $user_name.\"加入游戏\".\"<br/>\";
 }
    //创建静态函数,每个人充值的费用
 static function join_game($user_money){
  self::$user_money+=$user_money;
 }
 //返回静态函数
 static function return_join_game(){
  return self::$user_money;
 }
 public function user_age($user_age){
  $this->user_age=$user_age;
  if($user_age<18){
   echo \"对不起\".$this->user_name.\"您还未满十八周岁无法加入游戏哦!\".\"<br/>\";
  }
  else{
   echo $this->user_name=$user_name.\"祝您游戏愉快哦!\".\"<br/>\";
  }
 }
 //统计当前在线的人数
 public static function total(){
  self::$number+=1;
 }
 //返回当前统计函数
 public static function return_total(){
  return self::$number;
 }
}


   //创建人物
 $user1=new child(\"张三\");
  //调用人物充值函数
  child::join_game(600);
//创建人物的年龄
 $user1->user_age=33;
//判断人物的年龄
$user1->user_age(33);
//统计当前的人数函数
child::total();

  //创建人物
 $user2=new child(\"李四\");
  //调用人物充值函数
  child::join_game(500);
//创建人物的年龄
 $user2->user_age=16;
//判断人物的年龄
$user2->user_age(16);
//统计当前的人数函数
child::total();

  //创建人物
 $user3=new child(\"王五\");
  //调用人物充值函数
  child::join_game(400);
//创建人物的年龄
 $user1->user_age=25;
//判断人物的年龄
$user1->user_age(25);
//统计当前的人数函数
child::total();

//调用静态函数,充值费用
echo \"一共充值了\".child::return_join_game().\"元\".\"<br/>\";
//调用当前在线人数
echo \"当前在线人数为:\".child::return_total().\"人\".\"<br/>\";

?>

猜你喜欢

转载自blog.csdn.net/luo2424348224/article/details/78551284