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

成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供驛城企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計、成都做網(wǎng)站、HTML5、小程序制作等業(yè)務(wù)。10年已為驛城眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計公司優(yōu)惠進(jìn)行中。
#t#首先我們來理解三個關(guān)鍵字: this,self,parent,從字面上比較好理解,是指這,自己,父親,呵呵,比較好玩了,我們先建立幾個概念,這三個關(guān)鍵字分別是用在什么地方 呢?我們初步解釋一下,this是指向當(dāng)前對象的指針(我們姑且用C里面的指針來看吧),self是指向當(dāng)前類的指針,parent是指向父類的指針。
這么說還不能很了解,那我們就根據(jù)實際的例子結(jié)合來講講。
(1) PHP5指針之this
- class UserName
- {
- //定義屬性
- private $name;
- //定義構(gòu)造函數(shù)
- function __construct( $name )
- {
- $this->name = $name; //這里已經(jīng)使用了this指針
- }
- //析構(gòu)函數(shù)
- function __destruct(){}
- //打印用戶名成員函數(shù)
- function printName()
- {
- print( $this->name ); //又使用了this指針
- }
- }
- //實例化對象
- $nameObject = new UserName( "heiyeluren" );
- //執(zhí)行打印
- $nameObject->printName(); //輸出: heiyeluren
- //第二次實例化對象
- $nameObject2 = new UserName( "PHP5" );
- //執(zhí)行打印
- $nameObject2->printName(); //輸出:PHP5
- ?>
我們看,上面的類分別在11行和20行使用了this指針,那么當(dāng)時this是指向誰呢?其實this是在實例化的時候來確定指向誰,比如***次實例化對象 的時候(25行),那么當(dāng)時this就是指向$nameObject對象,那么執(zhí)行18行的打印的時候就把print( $this->
(2)PHP5指針之self
首先我們要明確一點,self是指向類本身,也就是self是不指向任何已經(jīng)實例化的對象,一般self使用來指向類中的靜態(tài)變量。
- class Counter
- {
- //定義屬性,包括一個靜態(tài)變量
- private static $firstCount = 0;
- private $lastCount;
- //構(gòu)造函數(shù)
- function __construct()
- {
- $this->lastCount = ++selft::$firstCount; //使用self來調(diào)用靜態(tài)變量,使用self調(diào)用必須使用::(域運算符號)
- }
- //打印最次數(shù)值
- function printLastCount()
- {
- print( $this->lastCount );
- }
- }
- //實例化對象
- $countObject = new Counter();
- $countObject->printLastCount(); //輸出 1
- ?>
我們這里只要注意兩個地方,第6行和第12行。我們在第二行定義了一個靜態(tài)變量$firstCount,并且初始值為0,那么在12行的時候調(diào)用了這個值 得,使用的是self來調(diào)用,并且中間使用"::"來連接,就是我們所謂的域運算符,那么這時候我們調(diào)用的就是類自己定義的靜態(tài)變量$ frestCount,我們的靜態(tài)變量與下面對象的實例無關(guān),它只是跟類有關(guān),那么我調(diào)用類本身的的,那么我們就無法使用this來引用,可以使用 self來引用,因為self是指向類本身,與任何對象實例無關(guān)。換句話說,假如我們的類里面靜態(tài)的成員,我們也必須使用self來調(diào)用。
(3)PHP5指針之parent
我們知道parent是指向父類的指針,一般我們使用parent來調(diào)用父類的構(gòu)造函數(shù)。
- //基類
- class Animal
- {
- //基類的屬性
- public $name; //名字
- //基類的構(gòu)造函數(shù)
- public function __construct( $name )
- {
- $this->name = $name;
- }
- }
- //派生類
- class Person extends Animal //Person類繼承了Animal類
- {
- public $personSex; //性別
- public $personAge; //年齡
- //繼承類的構(gòu)造函數(shù)
- function __construct( $personSex, $personAge )
- {
- parent::__construct( "heiyeluren" ); //使用parent調(diào)用了父類的構(gòu)造函數(shù)
- $this->personSex = $personSex;
- $this->personAge = $personAge;
- }
- function printPerson()
- {
- print( $this->name. " is " .$this->personSex. ",this year " .$this->personAge );
- }
- }
- //實例化Person對象
- $personObject = new Person( "male", "21");
- //執(zhí)行打印
- $personObject->printPerson(); //輸出:heiyeluren is male,this year 21
- ?>
我們注意這么幾個細(xì)節(jié):成員屬性都是public的,特別是父類的,是為了供繼承類通過this來訪問。我們注意關(guān)鍵的地方,第25行:parent:: __construct( "heiyeluren" ),這時候我們就使用PHP5指針中的parent來調(diào)用父類的構(gòu)造函數(shù)進(jìn)行對父類的初始化,因為父類的成員都是public的,于是我們就能夠在繼承類中直接使用 this來調(diào)用。
名稱欄目:PHP5指針的類別介紹
當(dāng)前鏈接:http://www.5511xx.com/article/djiieic.html


咨詢
建站咨詢
