<?php
interface Shape
{
public function area();
public function perimeter();
}
class Circle implements Shape
{
private $radius;
public function __construct($radius)
{
$this->radius = $radius;
}
public function area()
{
return $this->radius * $this->radius * pi();
}
public function perimeter()
{
return 2 * $this->radius * pi();
}
}
class Rectangle implements Shape
{
private $width;
private $height;
public function __construct($width, $height)
{
$this->width = $width;
$this->height = $height;
}
public function area()
{
return $this->width * $this->height;
}
public function perimeter()
{
return ($this->width + $this->height) * 2;
}
}
$circle1 = new Circle(5);
$circle2 = new Circle(9);
$rectangle1 = new Rectangle(5, 20);
$rectangle2 = new Rectangle(7, 28);
$shapes = [$circle1, $circle2, $rectangle1, $rectangle2];
foreach ($shapes as $shape) {
echo '<br>area: ' . $shape->area();
echo '<br>perimeter: ' . $shape->perimeter();
echo '<br>----------------------------------';
}
?>
area: 78.539816339745
perimeter: 31.415926535898
----------------------------------
area: 254.46900494077
perimeter: 56.548667764616
----------------------------------
area: 100
perimeter: 50
----------------------------------
area: 196
perimeter: 70
----------------------------------