舉例如下
<?php
class Demo {
function __construct() {
echo "constructor call...\n";
}
function __destruct() {
echo "destructor call...\n";
}
function do_something() {
echo "do something\n";
}
}
$o = new Demo();
$o->do_something();
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:class03.php
功能:示範 PHP 程式
作者:張凱慶
時間:西元 2013 年 2 月 */
?>建構子為預設名稱 __construct() ,注意開頭是連續兩個底線符號
function __construct() {
echo "constructor call...\n";
}解構子也是預設名稱 __destruct() ,注意開頭也是連續兩個底線符號
function __destruct() {
echo "destructor call...\n";
}執行結果如下

| 中英文術語對照 | |
|---|---|
| 建構子 | constructor |
| 物件 | object |
| 方法 | method |
| 解構子 | destructor |
您可以繼續參考
類別與物件
相關目錄
回 PHP 快速導覽
回 PHP 教材
回首頁
參考資料
http://www.php.net/manual/en/language.oop5.decon.php
沒有留言:
張貼留言