理解php5中的this,self,parent關鍵字用法

來源:互聯網
上載者:User
  1. class UserName

  2. {
  3. //定義屬性
  4. private $name;
  5. //定義建構函式
  6. function __construct( $name ){
  7. $this->name = $name; //這裡已經使用了this指標
  8. }

  9. //解構函式

  10. function __destruct(){}
  11. //列印使用者名稱成員函數
  12. function printName(){
  13. print( $this->name ); //又使用了this指標
  14. }
  15. }

  16. //執行個體化對象

  17. $nameObject = new UserName( "heiyeluren" );
  18. //執行列印
  19. $nameObject->printName(); //輸出: heiyeluren
  20. //第二次執行個體化對象
  21. $nameObject2 = new UserName( "PHP5" );
  22. //執行列印
  23. $nameObject2->printName(); //輸出:PHP5
  24. ?>
複製代碼

上面的類分別在11行和20行使用了this指標,那麼當時this是指向誰呢?其實this是在執行個體化的時候來確定指向誰,比如第一次執行個體化對象 的時候(25行),那麼當時this就是指向$nameObject對象,那麼執行18行的列印的時候就把print( $this->name ),那麼當然就輸出了"heiyeluren"。第二個執行個體,print( $this->name )變成了print( $nameObject2->name ),於是就輸出了"PHP5"。所以說,this就是指向當前對象執行個體的指標,不指向任何其他對象或類。

(2)self首先,明確一點,self是指向類本身,也就是self是不指向任何已經執行個體化的對象,一般self使用來指向類中的靜態變數。

  1. class Counter

  2. {
  3. //定義屬性,包括一個靜態變數
  4. private static $firstCount = 0;
  5. private $lastCount;

  6. //建構函式

  7. function __construct(){
  8. $this->lastCount = ++selft::$firstCount; //使用self來調用靜態變數,使用self調用必須使用::(域運算子號)
  9. }

  10. //列印最次數值

  11. function printLastCount(){
  12. print( $this->lastCount );
  13. }
  14. }

  15. //執行個體化對象

  16. $countObject = new Counter();
  17. $countObject->printLastCount(); //輸出 1
  18. ?>

複製代碼

注意兩個地方:第6行和第12行。在第二行定義了一個靜態變數$firstCount,並且初始值為0,那麼在12行的時調用了這個值,使用的是self來調用,並且中間使用"::"來串連,就是所謂的域運算子,那麼這時調用的就是類自己定義的靜態變數$frestCount,靜態變數與下面對象的執行個體無關,它只是跟類有關,那麼我調用類本身的,就無法使用this來引用,可以使用 self來引用,因為self是指向類本身,與任何對象執行個體無關。換句話說,假如要使用類裡面的靜態成員,也必須使用self來調用。

(3)、parent我們知道parent是指向父類的指標,一般使用parent來調用父類的建構函式。

  1. //基類

  2. class Animal
  3. {
  4. //基類的屬性
  5. public $name; //名字
  6. //基類的建構函式
  7. public function __construct( $name ){
  8. $this->name = $name;
  9. }
  10. }

  11. //衍生類別

  12. class Person extends Animal //Person類繼承了Animal類
  13. {
  14. public $personSex; //性別
  15. public $personAge; //年齡
  16. //繼承類的建構函式
  17. function __construct( $personSex, $personAge ){
  18. parent::__construct( "heiyeluren" ); //使用parent調用了父類的建構函式
  19. $this->personSex = $personSex;
  20. $this->personAge = $personAge;
  21. }
  22. function printPerson(){
  23. print( $this->name. " is " .$this->personSex. ",this year " .$this->personAge );
  24. }
  25. }

  26. //執行個體化Person對象

  27. $personObject = new Person( "male", "21");
  28. //執行列印
  29. $personObject->printPerson(); //輸出:heiyeluren is male,this year 21
  30. ?>

複製代碼

注意細節:成員屬性都是public的,特別是父類的,是為了供繼承類通過this來訪問。注意關鍵:第25行:parent:: __construct( "heiyeluren" ),這時我們就使用parent來調用父類的建構函式進行對父類的初始化,因為父類的成員都是public的,於是我們就能夠在繼承類中直接使用this來調用。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.