跟著百度學PHP[4]OOP面對對象編程-6-構造方法(__construct)和構析方法(__destruct)

來源:互聯網
上載者:User

標籤:color   ges   pre   des   .com   ons   function   成員方法   內容   

函數就是成員方法(方法有三:構造方法、成員方法、析構方法)

下面是兩種方法。

構造方法和構析方法

00x1 構造方法

構造方法會在建立對象之後自動調用。其名稱為__construct

<?php class Student{    var $name;    var $age;    var $sex;    function __construct(){    //可以從輸出中看得出來,我們並沒有輸出這個方法,但是他自動輸出了。這個就是構造方法的作用,會自動的被調用!        echo "520PHP<br />";     }    function study(){        echo "學習中,請稍後!";    }}    $daniu = new Student;    $daniu -> name="大牛夢!";    echo $daniu->name; ?>

 輸出效果:

00x2 構析方法

構析方法與構造方法相反。解構函式是PHP5新添加的內容,在PHP4中沒有解構函式。構析方法的名稱必須為__destruct,且構析方法不能帶有參數(次點與構造方法不相同,構造方法可以有參數)

 

 

 

 

 

 

 

 

 

THE END

 

跟著百度學PHP[4]OOP面對對象編程-6-構造方法(__construct)和構析方法(__destruct)

聯繫我們

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