PHP5 物件導向程式設計之:this, self, parent, static和const

來源:互聯網
上載者:User
 
 

PHP5 是一具備了大部分物件導向語言的特性的語言,比PHP4 有了很多的物件導向的特性,但是有部分概念也比較繞人,所以今天拿出來說說,說

的不好,請高手見諒. (閱讀本文,需要瞭解PHP5 的物件導向的知識)

首先我們來明白上面三個關鍵字: this,self,parent,從字面上比較好理解,是指這,自己,父親,呵呵,比較好玩了,我們先建立幾個概念,這三個

關鍵字分別是用在什麼地方呢?我們初步解釋一下,this 是指向當前對象的指標(我們姑且用C 裡面的指標來看吧),self 是指向當前類的指標

,parent 是指向父類的指標。我們這裡頻繁使用指標來描述,是因為沒有更好的語言來表達,呵呵,語文沒學好。 -_-#

這麼說還不能很瞭解,那我們就根據實際的例子結合來講講。

(1)this

———— PHP代碼 ————

1 2
3 class UserName
4 {
5     //定義屬性
6     private $name;
7
8     //定義建構函式
9     function __construct( $name )
10    {
11         $this->name = $name; //這裡已經使用了this 指標
12     }
13
14     //解構函式
15     function __destruct(){}
16
17     //列印使用者名稱成員函數
18     function printName()
19     {
20         print( $this->name ); //又使用了this 指標
21     }
22 }
23
24 //執行個體化對象
25 $nameObject = new UserName( "heiyeluren" );
26
27 //執行列印
28 $nameObject->printName(); //輸出: heiyeluren
29
30 //第二次執行個體化對象
31 $nameObject2 = new UserName( "PHP5" );
32
33 //執行列印
34 $nameObject2->printName(); //輸出:PHP5
35 ?>


————————————

我們看,上面的類分別在11 行和20 行使用了this 指標,那麼當時this 是指向誰呢?其實this 是在執行個體化的
時候來確定指向誰,比如第一次執行個體化對象的時候(25 行),那麼當時this 就是指向$nameObject 對象,那麼執
行18 行的列印的時候就把print( $this->name ),那麼當然就輸
出了"heiyeluren"。第二個執行個體的時候,print( $this->name )變成了print( $nameObject2->name ),
於是就輸出了"PHP5"。所以說,this 就是指向當前對象執行個體的指標,不指向任何其他對象或類。

(2)self

首先我們要明確一點,self 是指向類本身,也就是self 是不指向任何已經執行個體化的對象,一般self 使用來指向類中的靜態變數。

———— PHP代碼 ————

1 2
3 class Counter
4 {
5     //定義屬性,包括一個靜態變數
6     private static $firstCount = 0;
7     private $lastCount;
8
9     //建構函式
10    function __construct()
11    {
12         $this->lastCount = ++self::$firstCount; //使用self 來調用靜態變數,
                            //使用self 調用必須使用::(域運算子號)
13    }
14
15     //列印最次數值
16     function printLastCount()
17     {
18         print( $this->lastCount );
19     }
20 }
21
22 //執行個體化對象
23 $countObject = new Counter();
24
25 $countObject->printLastCount(); //輸出 1
26
27 ?>


————————————

我們這裡只要注意兩個地方,第6 行和第12 行。我們在第二行定義了一個靜態變數$firstCount,並且初始值為0,那麼在12 行的時候調用了

這個值得,使用的是self 來調用,並且中間使用"::"來串連,就是我們所謂的域運算子,那麼這時候我們調用的就是類自己定義的靜態變數

$frestCount,我們的靜態變數與下面對象的執行個體無關,它只是跟類有關,那麼我調用類本身的的,那麼我們就無法使用this 來引用,可以使

用self 來引用,因為self是指向類本身,與任何對象執行個體無關。換句話說,假如我們的類裡面靜態成員,我們也必須使用self

來調用。

(3)parent

我們知道parent 是指向父類的指標,一般我們使用parent 來調用父類的建構函式。

———— PHP代碼 ————

1 2
3 //基類
4 class Animal
5 {
6     //基類的屬性
7     public $name; //名字
8
9     //基類的建構函式
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     //繼承類的建構函式
23    function __construct( $personSex, $personAge )
24     {
25         parent::__construct( "heiyeluren" ); //使用parent 調用了父類的建構函式
26         $this->personSex = $personSex;
27         $this->personAge = $personAge;
28     }
29
30     function printPerson()
31     {
32         print( $this->name. " is " .$this->personSex. ",this year" .$this->personAge );
33     }
34 }
35
36 //執行個體化Person 對象
37 $personObject = new Person( "male", "21");
38
39//執行列印
40 $personObject->printPerson(); //輸出:
41
42 ?>


————————————

我們注意這麼幾個細節:成員屬性都是public 的,特別是父類的,是為了供繼承類通過this 來訪問。我們注意關鍵的地方,第25 行:

parent::__construct( "heiyeluren" ),這時候我們就使用parent 來調用父類的
建構函式進行對父類的初始化,因為父類的成員都是public 的,於是我們就能夠在繼承類中直接使用this 來調用。
總結:
this 是指向對象執行個體的一個指標,self 是對類本身的一個引用,parent 是對父類的引用。

(4) static

static 關鍵字在類中是,描述一個成員是靜態,static 能夠限制外部的訪問,因為static 後的成員是屬於
類的,是不屬於任何對象執行個體,其他類是無法訪問的,只對類的執行個體共用,能一定程式對該成員盡心保護。類的靜
態變數,非常類似全域變數,能夠被所有類的執行個體共用,類的靜態方法也是一樣的,類似於全域函數。類的靜態方
法能訪問類的靜態屬性。另外說明的是,static 的成員,必須使用self 來訪問,使用this 會出錯。
另外,還有一個比較重要的特點就是,如果一個方法使用了static,那麼這個方法就能夠在不需要執行個體化對象的前
提下直接使用該方法.
(關於this 和self 的異同,請參考:
http://blog.csdn.net/heiyeshuwu/archive/2004/11/03/165828.aspx )

(5)const

const 是一個定義常量的關鍵字,類似於C 中的#define,能夠定義一個常量,如果在程式中改變了它的值,那麼
會出現錯誤。

舉例說明上面的代碼:(註:以下代碼來自phpe.net)

———— PHP代碼 ————

class Counter
{
    private static $count = 0;//定義一個靜態屬性
    const   VERSION       = 2.0;//定義一個常量

    //建構函式
    function __construct()
    {
        self::$count++;
    }

    //解構函式
    function __destruct()
    {
        self::$count--;
    }

    //定義一個靜態方法
    static function getCount()
    {
        return self::$count;
    }
}

//建立一個執行個體
$c = new Counter();

//執行列印
print( Counter::getCount(). "
n" ); //使用直接輸入類名來訪問靜態方
法Counter::getCount

//列印類的版本
print( "Version useed: " .Counter::VERSION. "
n" );
?>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.