自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

PHP5指針的類別介紹

開發(fā) 后端
PHP5指針的類別分為:this,self,parent。我們今天就以實(shí)際例子來具體講解這三個(gè)指針的具體應(yīng)用方式,加深大家對他們的理解。

大家也許多PHP5已經(jīng)有所了解,但是關(guān)于PHP5指針又能了解多少呢?今天向大家介紹的是PHP5指針的三種類別,分別為:this,self,parent。PHP5是一具備了大部分面向?qū)ο笳Z言的特性的語言,比PHP4有了很多的面向?qū)ο蟮奶匦?但是有部分概念也比較繞人,所以今天拿出來說說,說的不好,請高手見諒.。

#t#首先我們來理解三個(gè)關(guān)鍵字: this,self,parent,從字面上比較好理解,是指這,自己,父親,呵呵,比較好玩了,我們先建立幾個(gè)概念,這三個(gè)關(guān)鍵字分別是用在什么地方 呢?我們初步解釋一下,this是指向當(dāng)前對象的指針(我們姑且用C里面的指針來看吧),self是指向當(dāng)前類的指針,parent是指向父類的指針。

這么說還不能很了解,那我們就根據(jù)實(shí)際的例子結(jié)合來講講。

(1) PHP5指針之this

  1. <?php   
  2.  
  3. class UserName  
  4. {   
  5. //定義屬性   
  6. private $name;  
  7.  
  8. //定義構(gòu)造函數(shù)  
  9. function __construct( $name )  
  10. {  
  11. $this->name = $name; //這里已經(jīng)使用了this指針  
  12. }  
  13.  
  14. //析構(gòu)函數(shù)  
  15. function __destruct(){}  
  16.  
  17. //打印用戶名成員函數(shù)  
  18. function printName()  
  19. {  
  20. print( $this->name ); //又使用了this指針  
  21. }  
  22. }  
  23.  
  24. //實(shí)例化對象  
  25. $nameObject = new UserName( "heiyeluren" );  
  26.  
  27. //執(zhí)行打印  
  28. $nameObject->printName(); //輸出: heiyeluren  
  29.  
  30. //第二次實(shí)例化對象  
  31. $nameObject2 = new UserName( "PHP5" );  
  32.  
  33. //執(zhí)行打印  
  34. $nameObject2->printName(); //輸出:PHP5  
  35. ?> 

我們看,上面的類分別在11行和20行使用了this指針,那么當(dāng)時(shí)this是指向誰呢?其實(shí)this是在實(shí)例化的時(shí)候來確定指向誰,比如***次實(shí)例化對象 的時(shí)候(25行),那么當(dāng)時(shí)this就是指向$nameObject對象,那么執(zhí)行18行的打印的時(shí)候就把print( $this-><name )變成了print( $nameObject->name ),那么當(dāng)然就輸出了"heiyeluren"。第二個(gè)實(shí)例的時(shí)候,print( $this->name )變成了print( $nameObject2->name ),于是就輸出了"PHP5"。所以說,this就是指向當(dāng)前對象實(shí)例的指針,不指向任何其他對象或類。

(2)PHP5指針之self

首先我們要明確一點(diǎn),self是指向類本身,也就是self是不指向任何已經(jīng)實(shí)例化的對象,一般self使用來指向類中的靜態(tài)變量。

  1. <?php 
  2.  
  3. class Counter  
  4. {  
  5. //定義屬性,包括一個(gè)靜態(tài)變量  
  6. private static $firstCount = 0;  
  7. private $lastCount;  
  8. //構(gòu)造函數(shù)  
  9. function __construct()  
  10. {  
  11. $this->lastCount = ++selft::$firstCount; //使用self來調(diào)用靜態(tài)變量,使用self調(diào)用必須使用::(域運(yùn)算符號)  
  12. }  
  13. //打印最次數(shù)值  
  14. function printLastCount()  
  15. {  
  16. print( $this->lastCount );  
  17. }   
  18. }  
  19.  
  20. //實(shí)例化對象  
  21. $countObject = new Counter();  
  22. $countObject->printLastCount(); //輸出 1  
  23. ?> 

我們這里只要注意兩個(gè)地方,第6行和第12行。我們在第二行定義了一個(gè)靜態(tài)變量$firstCount,并且初始值為0,那么在12行的時(shí)候調(diào)用了這個(gè)值 得,使用的是self來調(diào)用,并且中間使用"::"來連接,就是我們所謂的域運(yùn)算符,那么這時(shí)候我們調(diào)用的就是類自己定義的靜態(tài)變量$ frestCount,我們的靜態(tài)變量與下面對象的實(shí)例無關(guān),它只是跟類有關(guān),那么我調(diào)用類本身的的,那么我們就無法使用this來引用,可以使用 self來引用,因?yàn)閟elf是指向類本身,與任何對象實(shí)例無關(guān)。換句話說,假如我們的類里面靜態(tài)的成員,我們也必須使用self來調(diào)用。

(3)PHP5指針之parent

我們知道parent是指向父類的指針,一般我們使用parent來調(diào)用父類的構(gòu)造函數(shù)。

  1. <?php 
  2. //基類  
  3. class Animal  
  4. {  
  5. //基類的屬性  
  6. public $name; //名字  
  7. //基類的構(gòu)造函數(shù)  
  8. public function __construct( $name )  
  9. {  
  10. $this->name = $name;  
  11. }  
  12. }  
  13. //派生類  
  14. class Person extends Animal //Person類繼承了Animal類  
  15. {  
  16. public $personSex; //性別  
  17. public $personAge; //年齡  
  18. //繼承類的構(gòu)造函數(shù)  
  19. function __construct( $personSex, $personAge )  
  20. {  
  21. parent::__construct( "heiyeluren" ); //使用parent調(diào)用了父類的構(gòu)造函數(shù)  
  22. $this->personSex = $personSex;  
  23. $this->personAge = $personAge;  
  24. }  
  25. function printPerson()  
  26. {  
  27. print( $this->name. " is " .$this->personSex. ",this year " .$this->personAge );  
  28. }  
  29. }  
  30. //實(shí)例化Person對象  
  31. $personObject = new Person( "male", "21");  
  32.  
  33. //執(zhí)行打印  
  34. $personObject->printPerson(); //輸出:heiyeluren is male,this year 21  
  35.  
  36. ?> 
我們注意這么幾個(gè)細(xì)節(jié):成員屬性都是public的,特別是父類的,是為了供繼承類通過this來訪問。我們注意關(guān)鍵的地方,第25行:parent:: __construct( "heiyeluren" ),這時(shí)候我們就使用PHP5指針中的parent來調(diào)用父類的構(gòu)造函數(shù)進(jìn)行對父類的初始化,因?yàn)楦割惖某蓡T都是public的,于是我們就能夠在繼承類中直接使用 this來調(diào)用。
責(zé)任編輯:曹凱 來源: 機(jī)械工業(yè)出版社
相關(guān)推薦

2009-11-23 20:00:25

PHP5接口PHP5抽象類

2009-11-18 18:33:23

Linux PHP5安

2010-09-25 14:02:18

SQL INSERT

2009-11-17 14:01:01

Apache 2 PH

2009-11-18 14:45:02

PHP5 Sessio

2009-11-18 10:39:45

PHP5配置

2009-03-16 16:08:09

PHP異常

2009-11-24 17:01:39

PHP5多重繼承

2011-03-11 14:02:55

LAMP安裝PHP5

2009-11-24 16:28:41

PHP5魔術(shù)函數(shù)

2019-03-08 08:55:16

PHP7PHP5web安全

2009-12-11 17:33:56

PHP5常用函數(shù)

2009-12-03 13:50:16

PHP5異常處理

2009-11-23 19:33:12

PHP5多態(tài)性

2009-07-30 10:06:29

PHP5生成條形碼

2009-11-25 17:05:56

PHP5對象simpl

2009-11-23 16:43:03

PHP5安裝GD庫

2009-11-24 16:18:14

PHP5析構(gòu)函數(shù)

2009-11-23 13:44:33

PHP5面向?qū)ο?/a>

2009-11-23 19:42:16

PHP5平臺
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號