PHP OOP
Language: PHP
<?php
interface Shape
{
public function area(): float;
}
class Circle implements Shape
{
public function __construct(private float $radius)
{
}
public function area(): float
{
return 3.14159 * $this->radius ** 2;
}
}
class Square implements Shape
{
public function __construct(private float $side)
{
}
public function area(): float
{
return $this->side ** 2;
}
}
$shapes = [new Circle(2), new Square(3)];
foreach ($shapes as $shape) {
echo $shape->area() . "\n"; // 12.56636 then 9
}
?>Output
Click Run to execute this code.