PHP 靜態變數和靜態方法

來源:互聯網
上載者:User

標籤:elf   static   strong   echo   struct   sel   用法   const   copy   

PHP靜態變數
靜態變數(也叫類變數)

靜態變數的基本用法
1.在類中定義靜態變數形式:
[存取修飾詞] static $變數名;
例;  //public static $nums=0;
2.如何訪問靜態變數
如果在類中訪問  有兩種方法    self::$靜態變數名  ,  類名::$靜態變數名
如果在類外訪問: 有一種方法    類名::$靜態變數名

常式:
提出一個問題:
說,有一群小孩在玩堆雪人,不時有新的小孩加入,
請問如何知道現在共有多少人在玩?請使用面向
對象的思想,編寫程式解決

[php] view plain copy<?php  class Child  {      public $name;      //這裡定義並初始化一個靜態變數 $nums      public static $nums=0;      public static $i=1;      //建構函式      function __construct($name)      {                                $this->name=$name;      }       //靜態方法      public  function join_game()      {          //self::$nums 使用靜態變數          //self::$nums+=1;  或          Child::$nums+=1;          echo Child::$i.".".$this->name."加入堆雪人遊戲!<br>";          Child::$i++;      }  }  //建立三個小孩  $child1=new Child("李逵");  $child1->join_game();  $child2=new Child("張飛");  $child2->join_game();  $child3=new Child("唐僧");  $child3->join_game();  $child4=new Child("八戒");  $child4->join_game();  //看看有多少人蔘加了遊戲  echo "<br/> 共有--".child::$nums."--個人蔘加了遊戲!";  ?>  

靜態方法(又叫類方法)
其形式如下:
[存取修飾詞] static  function 方法名(){}
例;  //public static function enter_school($ifree){}
需求: 當我們操作靜態變數的時候,我們可以考慮使用靜態方法,比如統計所有學生交的學費

在我們編程中,我們往往使用靜態方法去操作靜態變數.

使用靜態方法不需要建立對象,可以直接存取該靜態方法

2.如何訪問靜態方法:
如果在類中訪問  有兩種方法    self::靜態方法名  ,  類名::靜態方法名
如果在類外訪問: 有兩種方法    類名::靜態方法名  ,  對象名->類方法名
靜態方法的特點
1.靜態方法只能操作靜態變數
2.靜態方法不能操作非靜態變數.
這裡請注意 : 普通的成員方法,既可以操作非靜態變數,也可以操作靜態變數 */

<?php  class student  {      public $name;      //這裡定義並初始化一個靜態變數 $nums      public static $free=0;      //public static $i=1;      //建構函式      function __construct($name,$ifree)      {                                $this->name=$name;          echo "<br>";          echo $this->name."入學了,要交學費:".$ifree."元<br>";      }       //靜態方法,新生入學,上交學費      public static function enter_school($ifree)      {          self::$free+=$ifree;          }      //擷取學費      public static function getfree()      {          return  self::$free;      }     }  //靜態方法不需要像普通成員方法一樣要建立對象才可調用,可在不建立對象的情況下調用靜態方法  //student::enter_school(10000);  //建立學生對象  $student1=new student("小明",1000);  //通過對象名調用靜態方法  $student1->enter_school(1000);  //通過類名來調用靜態方法  //student::enter_school(1000);  $student2=new student("小東",200);  $student2->enter_school(2000);  $student3=new student("小亮",3000);  $student3->enter_school(3000);    echo "共收取學費".$student3->getfree()."元!<br>";  //echo "共收取學費".student::getfree()."元!<br>";  ?>  

 

PHP 靜態變數和靜態方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.