Code: Select all
<?php
interface area2D{
public function get_area();
}
class shape2D{
private $length;
private $width;
function __construct( $slength = 1, $swidth = 1 ){
$this->set_length( $slength );
$this->set_width( $swidth );
}
public function set_length( $slength ){
$this->length = $slength;
}
public function set_width( $swidth ){
$this->width = $swidth;
}
public function get_length(){
return $this->length;
}
public function get_width(){
return $this->width;
}
}
class rectangle extends shape2D implements area2D{
function __construct( $rlength, $rwidth ){
parent::__construct( $rlength, $rwidth );
}
public function get_area(){
return $this->get_length() * $this->get_width();
}
}
class triangle extends shape2D implements area2D{
function __construct( $tlength, $theight ){
parent::__construct( $tlength, $theight );
}
public function get_area(){
return 0.5 * $this->get_length() * $this->get_width();
}
}
function show_area( shape2D $shape = null ){
if ( $shape instanceof shape2D )
echo $shape->get_area();
else
echo 'Not identified shape input';
}
$rec = new rectangle( 6, 10 );
show_area($rec);
$tri = new triangle ( 5, 20 );
show_area($tri);
?>