【PHP】this,self,parent的區別

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   ar   color   os   使用   

一. 定義&區別

self:  指向當前類的指標,self是不指向任何已經執行個體化的對象,一般self使用來指向類中的靜態變數。

this:  指向當前對象的指標,使用parent來調用父類的建構函式。

parent:  指向父類的指標

 

二.使用區別

 1.self

<?php    classcounter     //定義一個counter的類    {        //定義屬性,包括一個靜態變數$firstCount,並賦初值0 語句①          private static $firstCount = 0;        private $lastCount;        //建構函式        function __construct()        {             $this->lastCount =++self::$firstCount;      //使用self來調用靜態變數 語句②        }        //列印lastCount數值        function printLastCount()        {             print( $this->lastCount );        }    } //執行個體化對象  $obj = new Counter(); $obj->printLastCount();                             //執行到這裡的時候,程式輸出1 ?>
View Code

這裡要注意兩個地方語句①和語句②。我們在語句①定義了一個靜態變數$firstCount,那麼在語句②的時候使用了self調用這個值,那麼這時候我們調用的就是類自己定義的靜態變數$frestCount。我們的靜態變數與下面對象的執行個體無關,它只是跟類有關,那麼我調用類本身的的,那麼我們就無法使用this來引用,因為self是指向類本身,與任何對象執行個體無關。然後前面使用的this調用的是執行個體化的對象$obj,大家不要混淆了。

 

2.this

<?php classname          //建立了一個名為name的類 {    private$name;         //定義屬性,私人    //定義建構函式,用於初始化賦值    function __construct( $name )    {         $this->name =$name;         //這裡已經使用了this指標語句①    }    //解構函式    function __destruct(){}    //列印使用者名稱成員函數    function printname()    {         print( $this->name);             //再次使用了this指標語句②,也可以使用echo輸出    } } $obj1 = new name("PBPHome");   //執行個體化對象 語句③ //執行列印 $obj1->printname(); //輸出:PBPHome echo"<br>";                                    //輸出:斷行符號 //第二次執行個體化對象 $obj2 = new name( "PHP" ); //執行列印 $obj2->printname();                         //輸出:PHP ?>
View Code

說明:上面的類分別在 語句①和語句②使用了this指標,那麼當時this是指向誰呢?其實this是在執行個體化的時候來確定指向誰,比如第一次執行個體化對象的時候(語句③),那麼當時this就是指向$obj1對象,那麼執行語句②的列印時就把print( $this-><name ) 變成了 print($obj1t->name ),那麼當然就輸出了"PBPHome"。第二個執行個體的時候,print($this->name )變成了print( $obj2->name),於是就輸出了"PHP"。所以說,this就是指向當前對象執行個體的指標,不指向任何其他對象或類。

 

3.parent

<?php //建立基類Animal class Animal {    public $name; //基類的屬性,名字$name    //基類的建構函式,初始化賦值    public function __construct( $name )    {         $this->name = $name;    } } //定義衍生類別Person 繼承自Animal類 class Person extends Animal {    public$personSex;       //對於衍生類別,新定義了屬性$personSex性別、$personAge年齡    public $personAge;    //衍生類別的建構函式    function __construct( $personSex, $personAge )    {         parent::__construct( "PBPHome");    //使用parent調用了父類的建構函式 語句①         $this->personSex = $personSex;         $this->personAge = $personAge;    }    //衍生類別的成員函數,用於列印,格式:名字 is name,age is 年齡    function printPerson()    {         print( $this->name. " is ".$this->personSex. ",age is ".$this->personAge );     } } //執行個體化Person對象 $personObject = new Person( "male", "21"); //執行列印 $personObject->printPerson();//輸出結果:PBPHome is male,age is 21 ?>
View Code

 

 裡面同樣含有this的用法,大家自己分析。我們注意這麼個細節:成員屬性都是public(公有屬性和方法,類內部和外部的代碼均可訪問)的,特別是父類的,這是為了供繼承類通過this來訪問。關鍵點在語句①:parent::__construct( "heiyeluren"),這時候我們就使用parent來調用父類的建構函式進行對父類的初始化,這樣,繼承類的對象就都給賦值了name為PBPHome。我們可以測試下,再執行個體化一個對象$personObject1,執行列印後name仍然是PBPHome。

 

三.總結

this是指向對象執行個體的一個指標,在執行個體化的時候來確定指向;self是對類本身的一個引用,一般用來指向類中的靜態變數;parent是對父類的引用,一般使用parent來調用父類的建構函式。

this是指向對象執行個體的一個指標,self是對類本身的一個引用,parent是對父類的引用。

 

【PHP】this,self,parent的區別(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.