Code: Select all
<?php
class square { //สร้างคลาส
protected $width = '10'; //ประกาศ property
public $subject = 'ขนาดพื้นที่'; //^
protected $height = '5'; // ^
}
class triangle extends square { // extends เพื่อสืบทอดคลาส square
public function show() {
$area = ($this->width * $this->height)/2;
return $area;
}
}
$obj1=new triangle; //เรียกใช้ class
$obj=new square; //^
echo $obj->subject; //แสดงผล property ที่ใช้ visibility public
echo "</br>";
echo $obj1->show(); //เรียกใช้ method show
?>