構造函數:constructor
析構函數:destructor
在php5.1中,構造函數統一命名為:
function __construct(){
#函數體
}
析構函數統一命名為:
function __destruct(){
#函數體
}
注意,construct和destruct之前有兩個下劃線,不是一個
下面給出一個構造函數的示例:
<?php
class Car{
private $color;
private $size;
private $price;
function __construct(){
$this->color="red";
$this->size=4;
$this->price=30000;
}
public function display(){
echo $this->color."<br>";
echo $this->size."<br>";
echo $this->price."<br>";
}
}
$car=new Car();
$car->display();
?>
posted on 2009-06-28 09:53
期待明天 閱讀(297)
評論(0) 編輯 收藏 所屬分類:
PHP