Monday, July 22, 2013

[PHP][OOP] 類別中使用靜態方法

物件在實體化前,所有的方法都是無法使用的,除了靜態方法之外,未實體化也可以直些使用,直接看程式碼:(為了容易理解,我把class放上面)
class Test{  public $temp = '5678';   static function get_pw(){     return '1234';   } } echo Test::get_pw();  //印出1234

備註:
靜態方法中不能使用$this,例如:
static function get_pw(){   return $this -> temp; }
這是錯誤的。

No comments:

Post a Comment