Abstract Classes and Interfaces
An abstract class cannot be instantiated and can mix implemented and abstract methods. An interface lists method signatures and constants. A class can implement several interfaces.
Example
interface Shape { const SIDES = 0; public function area(): float; }
abstract class Base implements Shape { public function describe(): string { return static::class . " " . number_format($this->area(), 2); } }
class Circle extends Base { public function __construct(private float $r) {} public function area(): float { return M_PI * $this->r ** 2; } }
echo (new Circle(2))->describe(), "\n";Result
Circle 12.57
Exercise
Class that cannot be instantiated.
class Base {}Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.