Source Code:
(back to article)
<?php interface Shape { public function getArea(): float; } abstract class AbstractShape implements Shape { protected float $width; protected float $height; public function __construct(float $width, float $height) { $this->width = $width; $this->height = $height; } abstract public function getArea(): float; } class Rectangle extends AbstractShape { public function __construct(float $width, float $height) { parent::__construct($width, $height); } public function getArea(): float { return $this->width * $this->height; } } class Triangle extends AbstractShape { public function __construct(float $width, float $height) { parent::__construct($width, $height); } public function getArea(): float { return 0.5 * $this->width * $this->height; } } $rectangle = new Rectangle(5, 10); $triangle = new Triangle(5, 10); echo "The area of the rectangle is: " . $rectangle->getArea() . "\n"; echo "The area of the triangle is: " . $triangle->getArea() . "\n";
Result:
Report an issue