PHP5中的this,self和parent關(guān)鍵字詳解教程
更新時(shí)間:2007年03月19日 00:00:00 作者:
PHP是一具備了大部分面向?qū)ο笳Z(yǔ)言的特性的語(yǔ)言,比PHP有了很多的面向?qū)ο蟮奶匦?但是有部分概念也比較繞人,所以今天拿出來(lái)說(shuō)說(shuō),說(shuō)的不好,請(qǐng)高手見(jiàn)諒. (閱讀本文,需要了解PHP的面向?qū)ο蟮闹R(shí))
首先我們來(lái)明白上面三個(gè)關(guān)鍵字: this,self,parent,從字面上比較好理解,是指這,自己,父親,呵呵,比較好玩了,我們先建立幾個(gè)概念,這三個(gè)關(guān)鍵字分別是用在什么地方呢?我們初步解釋一下,this是指向當(dāng)前對(duì)象的指針(我們姑且用C里面的指針來(lái)看吧),self是指向當(dāng)前類(lèi)的指針,parent是指向父類(lèi)的指針。我們這里頻繁使用指針來(lái)描述,是因?yàn)闆](méi)有更好的語(yǔ)言來(lái)表達(dá),呵呵,語(yǔ)文沒(méi)學(xué)好。 -_-#
這么說(shuō)還不能很了解,那我們就根據(jù)實(shí)際的例子結(jié)合來(lái)講講。
(1) this
<?php
class UserName
{
//定義屬性
private $name;
//定義構(gòu)造函數(shù)
function __construct( $name )
{
$this->name = $name; //這里已經(jīng)使用了this指針
}
//析構(gòu)函數(shù)
function __destruct(){}
//打印用戶(hù)名成員函數(shù)
function printName()
{
print( $this->name ); //又使用了this指針
}
}
//實(shí)例化對(duì)象
$nameObject = new UserName( "heiyeluren" );
//執(zhí)行打印
$nameObject->printName(); //輸出: heiyeluren
//第二次實(shí)例化對(duì)象
$nameObject = new UserName( "PHP" );
//執(zhí)行打印
$nameObject->printName(); //輸出:PHP
?>
我們看,上面的類(lèi)分別在行和行使用了this指針,那么當(dāng)時(shí)this是指向誰(shuí)呢?其實(shí)this是在實(shí)例化的時(shí)候來(lái)確定指向誰(shuí),比如第一次實(shí)例化對(duì)象的時(shí)候(行),那么當(dāng)時(shí)this就是指向$nameObject對(duì)象,那么執(zhí)行行的打印的時(shí)候就把print( $this-><name )變成了print( $nameObject->name ),那么當(dāng)然就輸出了"heiyeluren"。第二個(gè)實(shí)例的時(shí)候,print( $this->name )變成了print( $nameObject->name ),于是就輸出了"PHP"。所以說(shuō),this就是指向當(dāng)前對(duì)象實(shí)例的指針,不指向任何其他對(duì)象或類(lèi)。
(2)self
首先我們要明確一點(diǎn),self是指向類(lèi)本身,也就是self是不指向任何已經(jīng)實(shí)例化的對(duì)象,一般self使用來(lái)指向類(lèi)中的靜態(tài)變量。
<?php
class Counter
{
//定義屬性,包括一個(gè)靜態(tài)變量
private static $firstCount = ;
private $lastCount;
//構(gòu)造函數(shù)
function __construct()
{
$this->lastCount = ++selft::$firstCount; //使用self來(lái)調(diào)用靜態(tài)變量,使用self調(diào)用必須使用::(域運(yùn)算符號(hào))
}
//打印最次數(shù)值
function printLastCount()
{
print( $this->lastCount );
}
}
//實(shí)例化對(duì)象
$countObject = new Counter();
$countObject->printLastCount(); //輸出
?>
我們這里只要注意兩個(gè)地方,第行和第行。我們?cè)诘诙卸x了一個(gè)靜態(tài)變量$firstCount,并且初始值為,那么在行的時(shí)候調(diào)用了這個(gè)值得,使用的是self來(lái)調(diào)用,并且中間使用"::"來(lái)連接,就是我們所謂的域運(yùn)算符,那么這時(shí)候我們調(diào)用的就是類(lèi)自己定義的靜態(tài)變量$frestCount,我們的靜態(tài)變量與下面對(duì)象的實(shí)例無(wú)關(guān),它只是跟類(lèi)有關(guān),那么我調(diào)用類(lèi)本身的的,那么我們就無(wú)法使用this來(lái)引用,可以使用self來(lái)引用,因?yàn)閟elf是指向類(lèi)本身,與任何對(duì)象實(shí)例無(wú)關(guān)。換句話說(shuō),假如我們的類(lèi)里面靜態(tài)的成員,我們也必須使用self來(lái)調(diào)用。
(3)parent
我們知道parent是指向父類(lèi)的指針,一般我們使用parent來(lái)調(diào)用父類(lèi)的構(gòu)造函數(shù)。
<?php
//基類(lèi)
class Animal
{
//基類(lèi)的屬性
public $name; //名字
//基類(lèi)的構(gòu)造函數(shù)
public function __construct( $name )
{
$this->name = $name;
}
}
//派生類(lèi)
class Person extends Animal //Person類(lèi)繼承了Animal類(lèi)
{
public $personSex; //性別
public $personAge; //年齡
//繼承類(lèi)的構(gòu)造函數(shù)
function __construct( $personSex, $personAge )
{
parent::__construct( "heiyeluren" ); //使用parent調(diào)用了父類(lèi)的構(gòu)造函數(shù)
$this->personSex = $personSex;
$this->personAge = $personAge;
}
function printPerson()
{
print( $this->name. " is " .$this->personSex. ",this year " .$this->personAge );
}
}
//實(shí)例化Person對(duì)象
$personObject = new Person( "male", "");
//執(zhí)行打印
$personObject->printPerson(); //輸出:heiyeluren is male,this year
?>
我們注意這么幾個(gè)細(xì)節(jié):成員屬性都是public的,特別是父類(lèi)的,是為了供繼承類(lèi)通過(guò)this來(lái)訪問(wèn)。我們注意關(guān)鍵的地方,第行:parent::__construct( "heiyeluren" ),這時(shí)候我們就使用parent來(lái)調(diào)用父類(lèi)的構(gòu)造函數(shù)進(jìn)行對(duì)父類(lèi)的初始化,因?yàn)楦割?lèi)的成員都是public的,于是我們就能夠在繼承類(lèi)中直接使用this來(lái)調(diào)用。
總結(jié):
this是指向?qū)ο髮?shí)例的一個(gè)指針,self是對(duì)類(lèi)本身的一個(gè)引用,parent是對(duì)父類(lèi)的引用。
基本上我所了解就這么多,肯定有理解錯(cuò)誤之處,請(qǐng)高手指出!
這么說(shuō)還不能很了解,那我們就根據(jù)實(shí)際的例子結(jié)合來(lái)講講。
(1) this
復(fù)制代碼 代碼如下:
<?php
class UserName
{
//定義屬性
private $name;
//定義構(gòu)造函數(shù)
function __construct( $name )
{
$this->name = $name; //這里已經(jīng)使用了this指針
}
//析構(gòu)函數(shù)
function __destruct(){}
//打印用戶(hù)名成員函數(shù)
function printName()
{
print( $this->name ); //又使用了this指針
}
}
//實(shí)例化對(duì)象
$nameObject = new UserName( "heiyeluren" );
//執(zhí)行打印
$nameObject->printName(); //輸出: heiyeluren
//第二次實(shí)例化對(duì)象
$nameObject = new UserName( "PHP" );
//執(zhí)行打印
$nameObject->printName(); //輸出:PHP
?>
我們看,上面的類(lèi)分別在行和行使用了this指針,那么當(dāng)時(shí)this是指向誰(shuí)呢?其實(shí)this是在實(shí)例化的時(shí)候來(lái)確定指向誰(shuí),比如第一次實(shí)例化對(duì)象的時(shí)候(行),那么當(dāng)時(shí)this就是指向$nameObject對(duì)象,那么執(zhí)行行的打印的時(shí)候就把print( $this-><name )變成了print( $nameObject->name ),那么當(dāng)然就輸出了"heiyeluren"。第二個(gè)實(shí)例的時(shí)候,print( $this->name )變成了print( $nameObject->name ),于是就輸出了"PHP"。所以說(shuō),this就是指向當(dāng)前對(duì)象實(shí)例的指針,不指向任何其他對(duì)象或類(lèi)。
(2)self
首先我們要明確一點(diǎn),self是指向類(lèi)本身,也就是self是不指向任何已經(jīng)實(shí)例化的對(duì)象,一般self使用來(lái)指向類(lèi)中的靜態(tài)變量。
復(fù)制代碼 代碼如下:
<?php
class Counter
{
//定義屬性,包括一個(gè)靜態(tài)變量
private static $firstCount = ;
private $lastCount;
//構(gòu)造函數(shù)
function __construct()
{
$this->lastCount = ++selft::$firstCount; //使用self來(lái)調(diào)用靜態(tài)變量,使用self調(diào)用必須使用::(域運(yùn)算符號(hào))
}
//打印最次數(shù)值
function printLastCount()
{
print( $this->lastCount );
}
}
//實(shí)例化對(duì)象
$countObject = new Counter();
$countObject->printLastCount(); //輸出
?>
我們這里只要注意兩個(gè)地方,第行和第行。我們?cè)诘诙卸x了一個(gè)靜態(tài)變量$firstCount,并且初始值為,那么在行的時(shí)候調(diào)用了這個(gè)值得,使用的是self來(lái)調(diào)用,并且中間使用"::"來(lái)連接,就是我們所謂的域運(yùn)算符,那么這時(shí)候我們調(diào)用的就是類(lèi)自己定義的靜態(tài)變量$frestCount,我們的靜態(tài)變量與下面對(duì)象的實(shí)例無(wú)關(guān),它只是跟類(lèi)有關(guān),那么我調(diào)用類(lèi)本身的的,那么我們就無(wú)法使用this來(lái)引用,可以使用self來(lái)引用,因?yàn)閟elf是指向類(lèi)本身,與任何對(duì)象實(shí)例無(wú)關(guān)。換句話說(shuō),假如我們的類(lèi)里面靜態(tài)的成員,我們也必須使用self來(lái)調(diào)用。
(3)parent
我們知道parent是指向父類(lèi)的指針,一般我們使用parent來(lái)調(diào)用父類(lèi)的構(gòu)造函數(shù)。
復(fù)制代碼 代碼如下:
<?php
//基類(lèi)
class Animal
{
//基類(lèi)的屬性
public $name; //名字
//基類(lèi)的構(gòu)造函數(shù)
public function __construct( $name )
{
$this->name = $name;
}
}
//派生類(lèi)
class Person extends Animal //Person類(lèi)繼承了Animal類(lèi)
{
public $personSex; //性別
public $personAge; //年齡
//繼承類(lèi)的構(gòu)造函數(shù)
function __construct( $personSex, $personAge )
{
parent::__construct( "heiyeluren" ); //使用parent調(diào)用了父類(lèi)的構(gòu)造函數(shù)
$this->personSex = $personSex;
$this->personAge = $personAge;
}
function printPerson()
{
print( $this->name. " is " .$this->personSex. ",this year " .$this->personAge );
}
}
//實(shí)例化Person對(duì)象
$personObject = new Person( "male", "");
//執(zhí)行打印
$personObject->printPerson(); //輸出:heiyeluren is male,this year
?>
我們注意這么幾個(gè)細(xì)節(jié):成員屬性都是public的,特別是父類(lèi)的,是為了供繼承類(lèi)通過(guò)this來(lái)訪問(wèn)。我們注意關(guān)鍵的地方,第行:parent::__construct( "heiyeluren" ),這時(shí)候我們就使用parent來(lái)調(diào)用父類(lèi)的構(gòu)造函數(shù)進(jìn)行對(duì)父類(lèi)的初始化,因?yàn)楦割?lèi)的成員都是public的,于是我們就能夠在繼承類(lèi)中直接使用this來(lái)調(diào)用。
總結(jié):
this是指向?qū)ο髮?shí)例的一個(gè)指針,self是對(duì)類(lèi)本身的一個(gè)引用,parent是對(duì)父類(lèi)的引用。
基本上我所了解就這么多,肯定有理解錯(cuò)誤之處,請(qǐng)高手指出!
您可能感興趣的文章:
- PHP編程過(guò)程中需要了解的this,self,parent的區(qū)別
- 深入php self與$this的詳解
- php class中self,parent,this的區(qū)別以及實(shí)例介紹
- php self,$this,const,static,->的使用
- php類(lèi)中的$this,static,final,const,self這幾個(gè)關(guān)鍵字使用方法
- 探討PHP中this,self,parent的區(qū)別詳解
- PHP中::、->、self、$this幾種操作符的區(qū)別介紹
- PHP中new static()與new self()的區(qū)別異同分析
- php 中self,this的區(qū)別和操作方法實(shí)例分析
相關(guān)文章
PHP個(gè)人網(wǎng)站架設(shè)連環(huán)講(二)
PHP個(gè)人網(wǎng)站架設(shè)連環(huán)講(二)...2006-10-10PHP的類(lèi) 功能齊全的發(fā)送郵件類(lèi)
前面我們已經(jīng)介紹過(guò)了一個(gè)用于群發(fā)郵件的類(lèi),那個(gè)類(lèi)的功能只能發(fā)文本格式的郵件,下面這個(gè)類(lèi)的功能則很強(qiáng)大,不但能發(fā)html格式的郵件,還可以發(fā)附件2006-10-10PHP分頁(yè)函數(shù)代碼(簡(jiǎn)單實(shí)用型)
PHP分頁(yè)函數(shù)代碼(簡(jiǎn)單實(shí)用型),學(xué)習(xí)php分頁(yè)的朋友可以參考下。2010-12-12