国产91福利在线观看,漂亮女保姆电影,欧美高h视频,青青青在线播放,96pao国产成视频永久免费,2022精品国偷自产免费观看,天天射天天爽

龍巖易富通網(wǎng)絡(luò)科技有限公司

龍巖小程序開(kāi)發(fā),龍巖分銷系統(tǒng)

this,self,parent三個(gè)關(guān)鍵字之間的區(qū)別

2015.09.10 | 38閱讀 | 0條評(píng)論 | 未命名

PHP5是一具備了大部分面向?qū)ο笳Z(yǔ)言的特性的語(yǔ)言,比PHP4有了很多的面向?qū)ο蟮奶匦?但是有部分概念也比較難以理解,這里我主要談的是 this,self,parent三個(gè)關(guān)鍵字之間的區(qū)別。從字面上比較好理解,分別是指這、自己、父親。我們先建立幾個(gè)概念,這三個(gè)關(guān)鍵字分別是用在什么 地方呢?我們初步解釋一下,this是指向當(dāng)前對(duì)象的指針(姑且用C里面的指針來(lái)看吧),self是指向當(dāng)前類的指針,parent是指向父類的指針。我 們這里頻繁使用指針來(lái)描述,是因?yàn)闆](méi)有更好的語(yǔ)言來(lái)表達(dá)。 這么說(shuō)還不能很了解,那我們就根據(jù)實(shí)際的例子結(jié)合來(lái)講講。 (1) 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-&gt;name ); //又使用了this指針 21???? } 22 } 23 24 //實(shí)例化對(duì)象 25 $nameObject = new UserName( "heiyeluren" ); 26 27 //執(zhí)行打印 28 $nameObject-&gt;printName(); //輸出: heiyeluren 29 30 //第二次實(shí)例化對(duì)象 31 $nameObject2 = new UserName( "PHP5" ); 32 33 //執(zhí)行打印 34 $nameObject2-&gt;printName(); //輸出:PHP5 35 ?&gt; 我 們看,上面的類分別在11行和20行使用了this指針,那么當(dāng)時(shí)this是指向誰(shuí)呢?其實(shí)this是在實(shí)例化的時(shí)候來(lái)確定指向誰(shuí),比如第一次實(shí)例化對(duì)象 的時(shí)候(25行),那么當(dāng)時(shí)this就是指向$nameObject對(duì)象,那么執(zhí)行18行的打印的時(shí)候就把print( $this-&gt;<name )變成了print( $nameObject->name ),那么當(dāng)然就輸出了"heiyeluren"。第二個(gè)實(shí)例的時(shí)候,print( $this-&gt;name )變成了print( $nameObject2-&gt;name ),于是就輸出了"PHP5"。所以說(shuō),this就是指向當(dāng)前對(duì)象實(shí)例的指針,不指向任何其他對(duì)象或類。 (2)self 首先我們要明確一點(diǎn),self是指向類本身,也就是self是不指向任何已經(jīng)實(shí)例化的對(duì)象,一般self使用來(lái)指向類中的靜態(tài)變量。 1 <?php 2 3???? class Counter 4???? { 5???????? //定義屬性,包括一個(gè)靜態(tài)變量 6???????? private static $firstCount = 0; 7???????? private $lastCount; 8 9???????? //構(gòu)造函數(shù) 10???????? function __construct() 11???????? { 12????????????? $this->lastCount = ++selft::$firstCount; //使用self來(lái)調(diào)用靜態(tài)變量,使用self調(diào)用必須使用::(域運(yùn)算符號(hào)) 13???????? } 14 15???????? //打印最次數(shù)值 16???????? function printLastCount() 17???????? { 18????????????? print( $this-&gt;lastCount ); 19???????? } 20???? } 21 22 //實(shí)例化對(duì)象 23 $countObject = new Counter(); 24 25 $countObject-&gt;printLastCount(); //輸出 1 26 27 ?&gt; 我 們這里只要注意兩個(gè)地方,第6行和第12行。我們?cè)诘诙卸x了一個(gè)靜態(tài)變量$firstCount,并且初始值為0,那么在12行的時(shí)候調(diào)用了這個(gè)值, 使用的是self來(lái)調(diào)用,并且中間使用"::"來(lái)連接,就是我們所謂的域運(yùn)算符,那么這時(shí)候我們調(diào)用的就是類自己定義的靜態(tài)變量$frestCount, 我們的靜態(tài)變量與下面對(duì)象的實(shí)例無(wú)關(guān),它只是跟類有關(guān),那么我調(diào)用類本身的的,那么我們就無(wú)法使用this來(lái)引用,可以使用 self來(lái)引用,因?yàn)閟elf是指向類本身,與任何對(duì)象實(shí)例無(wú)關(guān)。換句話說(shuō),假如我們的類里面靜態(tài)的成員,我們也必須使用self來(lái)調(diào)用。 (3)parent 我們知道parent是指向父類的指針,一般我們使用parent來(lái)調(diào)用父類的構(gòu)造函數(shù)。 1 <?php 2 3 //基類 4 class Animal 5 { 6???? //基類的屬性 7???? public $name; //名字 8 9???? //基類的構(gòu)造函數(shù) 10???? public function __construct( $name ) 11???? { 12????????? $this->name = $name; 13???? } 14 } 15 16 //派生類 17 class Person extends Animal //Person類繼承了Animal類 18 { 19???? public $personSex; //性別 20???? public $personAge; //年齡 21 22???? //繼承類的構(gòu)造函數(shù) 23???? function __construct( $personSex, $personAge ) 24???? { 25????????? parent::__construct( "heiyeluren" ); //使用parent調(diào)用了父類的構(gòu)造函數(shù) 26????????? $this-&gt;personSex = $personSex; 27????????? $this-&gt;personAge = $personAge; 28???? } 29 30???? function printPerson() 31???? { 32????????? print( $this-&gt;name. " is " .$this-&gt;personSex. ",this year " .$this-&gt;personAge ); 33????? } 34 } 35 36 //實(shí)例化Person對(duì)象 37 $personObject = new Person( "male", "21"); 38 39 //執(zhí)行打印 40 $personObject-&gt;printPerson(); //輸出:heiyeluren is male,this year 21 41 42 ?&gt; 我 們注意這么幾個(gè)細(xì)節(jié):成員屬性都是public的,特別是父類的,是為了供繼承類通過(guò)this來(lái)訪問(wèn)。我們注意關(guān)鍵的地方,第25行: parent::__construct( "heiyeluren" ),這時(shí)候我們就使用parent來(lái)調(diào)用父類的構(gòu)造函數(shù)進(jìn)行對(duì)父類的初始化,因?yàn)楦割惖某蓡T都是public的,于是我們就能夠在繼承類中直接使用 this來(lái)調(diào)用。 總結(jié): this是指向?qū)ο髮?shí)例的一個(gè)指針,self是對(duì)類本身的一個(gè)引用,parent是對(duì)父類的引用。
?
?
原文地址:http://hi.baidu.com/sneidar/blog/item/4c0015ecd4da9d38269791d3.html

贊 (

發(fā)表評(píng)論