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/>\";

?>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章