php魔術方法——建構函式和解構函式,php魔術建構函式_PHP教程

來源:互聯網
上載者:User

php魔術方法——建構函式和解構函式,php魔術建構函式


php有一類很神奇的方法,這些方法是保留方法,通常不會在外部被顯式調用,他們使用雙底線(__)開頭,他們被稱為魔術方法(Magic Methods)。php官方也不建議定義其他雙底線開頭的方法。

這次介紹最常見的魔術方法:建構函式和解構函式。

1. 建構函式(__construct)

void __construct ([ mixed $args [, $... ]] )

建構函式:擁有建構函式的類會在每次建立新對象時先調用此方法,所以非常適合在使用對象前做一些初始化服務。

注意:

  1. clone並不會調用建構函式

  2. 如果子類定義了建構函式,則不會隱式調用父類的建構函式

  3. 子類的建構函式允許和父類的建構函式參數不一致

  4. 如果子類沒有定義建構函式,php會嘗試尋找父類的建構函式

  5. 如果父類沒有定義建構函式,使用parent關鍵字顯式調用父類建構函式,會導致致命錯誤

 1 php 2  3 class P{ 4  5     public function __construct(){ 6         echo __CLASS__ . "\n"; 7     } 8  9 }10 11 class C1 extends P{12 13     public function __construct(){14         echo __CLASS__ . "\n";15     }16 17 }18 19 class C2 extends P{20 21     public function __construct(){22         parent::__construct();23         echo __CLASS__ . "\n";24     }25 26 }27 28 class C3 extends P{29 30 }31 32 // P33 $ins = new P();34 35 // Nothing36 $ins2 = clone $ins;37 38 // C139 new C1();40 41 // P42 // C243 new C2();44 45 // P46 new C3();

除了魔術方法的建構函式,php還支援與類名相同的建構函式,不過優先順序比魔術方法低:

 1 php 2  3 class C1{ 4  5     public function C1(){ 6         echo __CLASS__ . "1\n"; 7     } 8  9     public function __construct(){10         echo __CLASS__ . "2\n";11     }12 13 }14 15 class C2{16 17     public function C2(){18         echo __CLASS__ . "1\n";19     }20 21 }22 23 class C3{24 25     public function C3(){26         echo __CLASS__ . "1\n";27     }28 29     public function __construct(){30         echo __CLASS__ . "2\n";31         $this->C3();32     }33 34 }35 36 // C1237 new C1();38 39 // C2140 new C2();41 42 // C3243 // C3144 new C3();

php5.3.3之後,在命名空間之內使用與類名同名的方法,不再作為建構函式,命名空間之外不變:

 1 php 2  3 namespace N; 4  5 class C{ 6  7     public function C(){ 8         echo __CLASS__ . "\n"; 9     }   10 11 }12 13 // Nothing14 new \N\C();

建構函式可以用全部三個存取控制修飾符,如單例模式:

 1 php 2  3 class Single{ 4  5     public static function getInstance(){ 6         static $ins = null; 7         if(empty($ins)){ 8             $ins = new self(); 9         }   10         return $ins;11     }   12 13     private function __construct(){14         echo __CLASS__ . "\n";15     }   16 17 }18 19 // Single20 Single::getInstance();

2. 解構函式(__destruct)

void __destruct ( void )

解構函式:解構函式會在某個對象的引用被全部刪除或對象被顯示銷毀時執行。

注意:

  1. 同建構函式類似,父類的解構函式並不會被引擎暗中調用,必須顯式調用parent::__destruct

  2. exit和die並不能阻止解構函式的執行

  3. 致命錯誤會阻止解構函式的執行

  4. 在解構函式中調用exit,可以阻止其他未執行的解構函式的執行

  5. 如果父類沒有定義解構函式,使用parent關鍵字顯式調用父類析構函數,會導致致命錯誤

phpclass P{    function __destruct(){        echo get_class($this) . "\t" . __CLASS__ . "\n";    }   }class C1 extends P{    function __destruct(){        echo get_class($this) . "\t" . __CLASS__ . "\n";    }   }class C2 extends P{    function __destruct(){        parent::__destruct();        echo get_class($this) . "\t" . __CLASS__ . "\n";    }   }class C3 extends P{}$insP = new P();$ins1 = new C1();$ins2 = new C2();$ins3 = new C3();/**輸出:C3    PC2    PC2    C2C1    C1P    P**/

http://www.bkjia.com/PHPjc/1104060.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1104060.htmlTechArticlephp魔術方法——建構函式和解構函式,php魔術建構函式 php有一類很神奇的方法,這些方法是保留方法,通常不會在外部被顯式調用,他們使...

  • 聯繫我們

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