日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

php5中this_self_parent关键字用法讲解

發(fā)布時(shí)間:2025/3/17 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php5中this_self_parent关键字用法讲解 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

PHP從5開(kāi)始具備了大部分面向?qū)ο笳Z(yǔ)言的特性,比PHP4多了很多面向?qū)ο蟮奶匦?在此我們主要講解三個(gè)關(guān)鍵字: this,self,parent,從字面上比較好理解,是指這,自己,父親,我們先建立幾個(gè)概念,這三個(gè)關(guān)鍵字分別是用在什么地方呢?我們初步解釋一下,this是指向當(dāng)前對(duì)象的指針(我們姑且用C里面的指針來(lái)看吧),self是指向當(dāng)前類的指針,parent是指向父類的指針。
下面通過(guò)實(shí)例講解。
(1) this
<?php
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指針
}
}
//實(shí)例化對(duì)象
$nameObject = new UserName( "heiyeluren" );
//執(zhí)行打印
$nameObject->printName(); //輸出: heiyeluren
//第二次實(shí)例化對(duì)象
$nameObject2 = new UserName( "PHP5" );
//執(zhí)行打印
$nameObject2->printName(); //輸出:PHP5
?>
我 們看,上面的類分別在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-><name )變成了print( $nameObject->name ),那么當(dāng)然就輸出了"heiyeluren"。第二個(gè)實(shí)例的時(shí)候,print( $this->name )變成了print( $nameObject2->name ),于是就輸出了"PHP5"。所以說(shuō),this就是指向當(dāng)前對(duì)象實(shí)例的指針,不指向任何其他對(duì)象或類。

(2)self
首先我們要明確一點(diǎn),self是指向類本身,也就是self是不指向任何已經(jīng)實(shí)例化的對(duì)象,一般self使用來(lái)指向類中的靜態(tài)變量。
<?php
class Counter
{
//定義屬性,包括一個(gè)靜態(tài)變量
private static $firstCount = 0;
private $lastCount;
//構(gòu)造函數(shù)
function __construct(){
$this->lastCount = ++self::$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(); //輸出 1
?>

我 們這里只要注意兩個(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ù)。
<?php
//基類
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 );
}
}
//實(shí)例化Person對(duì)象
$personObject = new Person( "male", "21");
//執(zhí)行打印
$personObject->printPerson(); //輸出:heiyeluren is male,this year 21
?>
我們注意這么幾個(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)用。

轉(zhuǎn)載于:https://my.oschina.net/pengting/blog/99369

總結(jié)

以上是生活随笔為你收集整理的php5中this_self_parent关键字用法讲解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。