class 類別名稱01 {
權限 成員名稱01;
權限 成員名稱02;
權限 成員名稱03;
...
權限 方法01();
權限 方法02();
...
}
public:任何類別均可以存取資料,代表存取沒有任何限制。
protected:僅有這個類別與子類別 (subclass) 可以存取。
private:僅有這個類別本身可以存取。
<?php
class bmi {
public $name;
private $weight;
protected $height;
public function getdata($W,$H)
// public function getdata($N,$W,$H)
{
$this->width=$W;
$this->height=$H;
$h=$this->height/100;
return round($this->width/($h * $h), 1);
// return $this->name.":".round($this->width/($h * $h), 1);
}
}
$mybmi = new bmi();
echo "BMI=".$mybmi->getdata(68,175)."<br>";
// echo "BMI=".$mybmi->getdata('Bless',68,175)."<br>";
?>
<?php
class volume {
public $length;
private $weight;
protected $height;
public function getdata($L,$W,$H){
$this->length=$L;
$this->width=$W;
$this->height=$H;
$v=$this->length * $this->width * $this->height;
return $v;
} }
$myVolume = new volume();
echo "長方體的體積 = ".$myVolume->getdata(8,5,2)." cm<sup>3</sup><br>";
?>